wechatapi.class.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. class wechatapi
  3. {
  4. public $apiUrl = 'https://qyapi.weixin.qq.com/cgi-bin/';
  5. private $appKey;
  6. private $appSecret;
  7. private $token;
  8. private $agentId;
  9. private $expires;
  10. private $errors = array();
  11. /**
  12. * Construct
  13. *
  14. * @param string $appKey
  15. * @param string $appSecret
  16. * @param string $agentId
  17. * @param string $apiUrl
  18. * @access public
  19. */
  20. public function __construct($appKey, $appSecret, $agentId, $apiUrl = '')
  21. {
  22. $this->appKey = $appKey;
  23. $this->appSecret = $appSecret;
  24. $this->agentId = $agentId;
  25. if($apiUrl) $this->apiUrl = rtrim($apiUrl, '/') . '/';
  26. if(!$this->getToken()) return array('result' => 'fail', 'message' => $this->errors);
  27. }
  28. /**
  29. * Get token.
  30. *
  31. * @access public
  32. * @return string
  33. */
  34. public function getToken()
  35. {
  36. if($this->token and (time() - $this->expires) >= 0) return $this->token;
  37. $response = $this->queryAPI($this->apiUrl . "gettoken?corpid={$this->appKey}&corpsecret={$this->appSecret}");
  38. if($this->isError()) return false;
  39. $this->token = $response->access_token;
  40. $this->expires = time() + $response->expires_in;
  41. return $this->token;
  42. }
  43. /**
  44. * Get all users.
  45. *
  46. * @access public
  47. * @return array
  48. */
  49. public function getAllUsers()
  50. {
  51. $depts = $this->getAgent();
  52. if($this->isError()) return array('result' => 'fail', 'message' => $this->errors);
  53. $users = array();
  54. foreach($depts->deptList as $deptID)
  55. {
  56. $response = $this->queryAPI($this->apiUrl . "user/simplelist?access_token={$this->token}&department_id={$deptID}&fetch_child=1");
  57. if($this->isError()) return array('result' => 'fail', 'message' => $this->errors);
  58. foreach($response->userlist as $user) $users[$user->name] = $user->userid;
  59. }
  60. $users = array_merge($users, $depts->userList);
  61. return array('result' => 'success', 'data' => $users);
  62. }
  63. /**
  64. * Get agent details.
  65. *
  66. * @access public
  67. * @return stdClass
  68. */
  69. public function getAgent()
  70. {
  71. $response = $this->queryAPI($this->apiUrl . "agent/get?access_token={$this->token}&agentid={$this->agentId}");
  72. $deptInfo = new stdClass();
  73. $deptInfo->userList = array();
  74. if(isset($response->allow_userinfos))
  75. {
  76. foreach ($response->allow_userinfos->user as $user)
  77. {
  78. $userInfo = $this->queryAPI($this->apiUrl . "user/get?access_token={$this->token}&userid={$user->userid}");
  79. $deptInfo->userList[$userInfo->name] = $userInfo->userid;
  80. }
  81. }
  82. $deptInfo->deptList = array();
  83. if(isset($response->allow_partys)) $deptInfo->deptList = $response->allow_partys->partyid;
  84. if($this->isError()) return false;
  85. return $deptInfo;
  86. }
  87. /**
  88. * Send message
  89. *
  90. * @param string $userList
  91. * @param string $message
  92. * @access public
  93. * @return array
  94. */
  95. public function send($userList, $message)
  96. {
  97. $message = json_decode($message);
  98. $message->agentid = $this->agentId;
  99. $message->touser = str_replace(',', '|', $userList);
  100. $url = $this->apiUrl . 'message/send?access_token=' . $this->token;
  101. $response = common::http($url, json_encode($message), array(), array('Content-Type: text/plain'));
  102. $errors = commonModel::$requestErrors;
  103. $response = json_decode($response);
  104. if(isset($response->errcode) and $response->errcode == 0) return array('result' => 'success');
  105. if(empty($response)) $this->errors = $errors;
  106. if(isset($response->errcode)) $this->errors[$response->errcode] = "Errcode:{$response->errcode}, Errmsg:{$response->errmsg}";
  107. return array('result' => 'fail', 'message' => $this->errors);
  108. }
  109. /**
  110. * Query API.
  111. *
  112. * @param string $url
  113. * @access public
  114. * @return string
  115. */
  116. public function queryAPI($url)
  117. {
  118. $response = common::http($url);
  119. $errors = commonModel::$requestErrors;
  120. $response = json_decode($response);
  121. if(isset($response->errcode) and $response->errcode == 0) return $response;
  122. if(empty($response)) $this->errors = $errors;
  123. if(isset($response->errcode)) $this->errors[$response->errcode] = "Errcode:{$response->errcode}, Errmsg:{$response->errmsg}";
  124. return false;
  125. }
  126. /**
  127. * Check for errors.
  128. *
  129. * @access public
  130. * @return bool
  131. */
  132. public function isError()
  133. {
  134. return !empty($this->errors);
  135. }
  136. /**
  137. * Get errors.
  138. *
  139. * @access public
  140. * @return array
  141. */
  142. public function getErrors()
  143. {
  144. $errors = $this->errors;
  145. $this->errors = array();
  146. return $errors;
  147. }
  148. }