wxclient.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. namespace app\components\OpenAuth\core;
  3. use \Exception;
  4. use app\common\helpers\Session;
  5. use Yii;
  6. class Wxclient {
  7. private $APIMap;
  8. function __construct() {
  9. $this->oauth = new WX();
  10. /*
  11. * 初始化APIMap
  12. * 加#表示非必须,无则不传入url(url中不会出现该参数), "key" => "val" 表示key如果没有定义则使用默认值val
  13. * 规则 array( baseUrl, argListArr, method)
  14. */
  15. $this->APIMap = array(
  16. "get_user_info" => array(
  17. "https://api.weixin.qq.com/sns/userinfo",
  18. array("format" => "json"),
  19. "GET"
  20. )
  21. );
  22. }
  23. //调用相应api
  24. private function _applyAPI($arr, $argsList, $baseUrl, $method) {
  25. $pre = "#";
  26. $keysArr = array(
  27. "access_token" => Session::get('wx_token.access_token'),
  28. "openid" => Session::get('wx_token.openid')
  29. );
  30. if ($method == "POST") {
  31. $this->oauth->ssl_verifypeer = 0;
  32. $response = $this->oauth->https_request($baseUrl, null, $keysArr);
  33. } else if ($method == "GET") {
  34. $baseUrl.="?".http_build_query($keysArr);
  35. $response = $this->oauth->https_request($baseUrl);
  36. }
  37. return $response;
  38. }
  39. public function __call($name, $arg) {
  40. //如果APIMap不存在相应的api
  41. if (empty($this->APIMap[$name])) {
  42. throw new Exception("不存在的API: <span style='color:red;'>$name</span>");
  43. }
  44. //从APIMap获取api相应参数
  45. $baseUrl = $this->APIMap[$name][0];
  46. $argsList = $this->APIMap[$name][1];
  47. $method = isset($this->APIMap[$name][2]) ? $this->APIMap[$name][2] : "GET";
  48. if (empty($arg)) {
  49. $arg[0] = null;
  50. }
  51. //对于get_tenpay_addr,特殊处理,php json_decode对\xA312此类字符支持不好
  52. $responseArr = $this->obj2array($this->_applyAPI($arg[0], $argsList, $baseUrl, $method));
  53. //检查返回ret判断api是否成功调用
  54. if ($responseArr['ret'] == 0) {
  55. return $responseArr;
  56. } else {
  57. throw new Exception($responseArr['msg']);
  58. }
  59. }
  60. /**
  61. * 多级对象转数组
  62. * @param obj $object 待转换的对象
  63. * @return array
  64. */
  65. private function obj2array($object = NULL) {
  66. $array = (array) $object;
  67. foreach ($array as $key => $val) {
  68. //判断是否为对象或数组,因为数组中可能还会存在对象
  69. if (is_object($val) || is_array($val)) {
  70. $val = obj2array($val);
  71. }
  72. $array[$key] = $val;
  73. }
  74. return $array;
  75. }
  76. //php 对象到数组转换
  77. private function objToArr($obj) {
  78. if (!is_object($obj) && !is_array($obj)) {
  79. return $obj;
  80. }
  81. $arr = array();
  82. foreach ($obj as $k => $v) {
  83. $arr[$k] = $this->objToArr($v);
  84. }
  85. return $arr;
  86. }
  87. public function get_access_token() {
  88. return Session::get('wx_token.access_token');
  89. }
  90. //简单实现json到php数组转换功能
  91. private function simple_json_parser($json) {
  92. $json = str_replace("{", "", str_replace("}", "", $json));
  93. $jsonValue = explode(",", $json);
  94. $arr = array();
  95. foreach ($jsonValue as $v) {
  96. $jValue = explode(":", $v);
  97. $arr[str_replace('"', "", $jValue[0])] = (str_replace('"', "", $jValue[1]));
  98. }
  99. return $arr;
  100. }
  101. }