Home >Backend Development >PHP Tutorial >The latest and most comprehensive API interface of WeChat 5.0 developed based on EaglePHP framework v2.7

The latest and most comprehensive API interface of WeChat 5.0 developed based on EaglePHP framework v2.7

WBOY
WBOYOriginal
2016-07-25 08:49:07901browse
Code source: http://www.eaglephp.com
Applicable platform: window/Linux
Dependent projects: EaglePHP framework

Includes WeChat 5.0 API basic interface, custom menu, and advanced interface, as follows:
1. Receive user messages .
2. Reply to the user.
3. Accept event push.
4. Conversation interface custom menu.
5. Voice recognition.
6. Customer service interface.
7. OAuth2.0 web authorization.
8. Generate QR code with parameters.
9. Obtain the user’s geographical location.
10. Obtain basic user information.
11. Get the followers list.
12. User grouping.
  1. /**
  2. * WeChat public platform API
  3. *
  4. * @author maojianlw@139.com
  5. * [url=home.php?mod=space&uid=17823]@LINK[/url] http://www.eaglephp.com
  6. */
  7. class WeixinChat
  8. {
  9. private $token;
  10. private $appid;
  11. private $appsecret;
  12. private $access_token;
  13. // Received data
  14. private $_receive = array();
  15. private $_reply = '';
  16. // Interface error code
  17. private $errCode = '';
  18. // Interface error message
  19. private $errMsg = '' ;
  20. // Log in with WeChat oauth to get the code
  21. const CONNECT_OAUTH_AUTHORIZE_URL = 'https://open.weixin.qq.com/connect/oauth2/authorize?';
  22. // Log in with WeChat oauth and exchange the code for webpage authorization access_token
  23. const SNS_OAUTH_ACCESS_TOKEN_URL = 'https://api.weixin.qq.com/sns/oauth2/access_token?';
  24. // WeChat oauth login refresh access_token (if necessary)
  25. const SNS_OAUTH_REFRESH_TOKEN_URL = 'https://api.weixin.qq .com/sns/oauth2/refresh_token?';
  26. // Exchange ticket for QR code
  27. const SHOW_QRCODE_URL = 'https://mp.weixin.qq.com/cgi-bin/showqrcode?';
  28. // WeChat oauth login pulls user information (needs scope is snsapi_userinfo)
  29. const SNS_USERINFO_URL = 'https://api.weixin.qq.com/sns/userinfo?';
  30. // Request api prefix
  31. const API_URL_PREFIX = 'https: //api.weixin.qq.com/cgi-bin';
  32. // Custom menu creation
  33. const MENU_CREATE_URL = '/menu/create?';
  34. // Custom menu query
  35. const MENU_GET_URL = '/menu /get?';
  36. // Custom menu deletion
  37. const MENU_DELETE_URL = '/menu/delete?';
  38. // Get access_token
  39. const AUTH_URL = '/token?grant_type=client_credential&';
  40. // Get user Basic information
  41. const USER_INFO_URL = '/user/info?';
  42. // Get the follower list
  43. const USER_GET_URL = '/user/get?';
  44. // Query the group
  45. const GROUPS_GET_URL = '/groups/get? ';
  46. // Create a group
  47. const GROUPS_CREATE_URL = '/groups/create?';
  48. // Modify the group name
  49. const GROUPS_UPDATE_URL = '/groups/update?';
  50. // Move user groups
  51. const GROUPS_MEMBERS_UPDATE_URL = '/groups/members/update?';
  52. //Send customer service message
  53. const MESSAGE_CUSTOM_SEND_URL = '/message/custom/send?';
  54. //Create QR code ticket
  55. const QRCODE_CREATE_URL = '/qrcode/create? ';
  56. /**
  57. * Initialization configuration data
  58. * @param array $options
  59. */
  60. public function __construct($options)
  61. {
  62. $this->token = isset($options['token']) ? $options['token'] : '';
  63. $this->appid = isset($options['appid']) ? $options['appid'] : '';
  64. $this->appsecret = isset($options['appsecret' ]) ? $options['appsecret'] : '';
  65. }
  66. /**
  67. * Get incoming messages
  68. * When an ordinary WeChat user sends a message to a public account, the WeChat server will POST the XML data packet of the message to the URL filled in by the developer.
  69. */
  70. public function getRev()
  71. {
  72. $postStr = file_get_contents('php://input');
  73. if($postStr)
  74. {
  75. $this->_receive = (array)simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
  76. //Log::info(var_export($this->_receive, true) );
  77. }
  78. return $this;
  79. }
  80. /**
  81. * Get messages sent from WeChat server
  82. */
  83. public function getRevData()
  84. {
  85. return $this->_receive;
  86. }
  87. /**
  88. * Get receiver
  89. */
  90. public function getRevTo()
  91. {
  92. return isset($this->_receive['ToUserName']) ? $this->_receive['ToUserName'] : false;
  93. }
  94. /**
  95. * Get the message sender (an OpenID)
  96. */
  97. public function getRevFrom()
  98. {
  99. return isset($this->_receive['FromUserName']) ? $this->_receive['FromUserName'] : false;
  100. }
  101. /* *
  102. * Get the creation time of the received message (integer type)
  103. */
  104. public function getRevCTime()
  105. {
  106. return isset($this->_receive['CreateTime']) ? $this->_receive['CreateTime'] : false;
  107. }
  108. /**
  109. * Get the received message type (text, image, voice, video, location, link, event)
  110. */
  111. public function getRevType()
  112. {
  113. return isset($this->_receive['MsgType']) ? $this->_receive['MsgType'] : false;
  114. }
  115. /**
  116. * Get the received message number
  117. */
  118. public function getRevId()
  119. {
  120. return isset($this->_receive['MsgId']) ? $this->_receive['MsgId'] : false;
  121. }
  122. /**
  123. * Get the received message text
  124. * Through the speech recognition interface, the voice sent by the user will also give the text content recognized by the speech recognition.(You need to apply for the advanced interface permission of the service account)
  125. */
  126. public function getRevText()
  127. {
  128. if(isset($this->_receive['Content'])) return trim($this->_receive[' Content']);
  129. elseif(isset($this->_receive['Recognition'])) return trim($this->_receive['Recognition']);
  130. else return false;
  131. }
  132. / **
  133. * Get and receive picture messages
  134. */
  135. public function getRevImage()
  136. {
  137. if(isset($this->_receive['PicUrl'])){
  138. return array(
  139. 'picUrl' => $this-> _receive['PicUrl'], //Picture link
  140. 'mediaId' => $this->_receive['MediaId'] //Picture message media id, you can call the multimedia file download interface to pull the data. }
  141. return false;
  142. }
  143. /**
  144. * Get and receive voice messages
  145. */
  146. public function getRevVoice()
  147. {
  148. if(isset($this->_receive['MediaId'])){
  149. return array(
  150. 'mediaId' => $this->_receive['MediaId'], //Voice message media id, you can call the multimedia file download interface to pull data.
  151. 'format' => $this->_receive[' Format'] //Voice format, such as amr, speex, etc.
  152. );
  153. }
  154. return false;
  155. }
  156. /**
  157. *Get to receive video messages
  158. */
  159. public function getRevVideo()
  160. {
  161. if(isset($this ->_receive['MediaId'])){
  162. return array(
  163. 'mediaId' => $this->_receive['MediaId'], //Video message media id, you can call the multimedia file download interface to pull it data.
  164. 'thumbMediaId' => $this->_receive['ThumbMediaId'] //The media ID of the video message thumbnail, you can call the multimedia file download interface to pull the data.
  165. );
  166. }
  167. return false;
  168. }
  169. /**
  170. * Get the user’s geographical location
  171. */
  172. public function getRevLocation()
  173. {
  174. if(isset($this->_receive['Location_X'])){
  175. return array(
  176. 'locationX' => $this->_receive['Location_X'], //Geographical location dimension
  177. 'locationY' => $this->_receive['Location_Y'], //Geographical location Longitude
  178. 'scale' => $this->_receive['Scale'], //Map zoom size
  179. 'label' => $this->_receive['Label'] //Geographical location information
  180. ) ;
  181. }
  182. //For a public account that has opened a geographical location reporting interface, when a user enters the public account session after following it, a box will pop up asking the user to confirm whether the public account is allowed to use its geographical location.
  183. //The pop-up box only appears once after following it. Users can operate on the official account details page in the future.
  184. elseif(isset($this->_receive['Latitude']))
  185. {
  186. return array(
  187. 'latitude' => $this->_receive['Latitude'], //Geographical location latitude
  188. ' longitude' => $this->_receive['Longitude'], //Geographical location longitude
  189. 'precision' => $this->_receive['Precision'] //Geographical location precision
  190. );
  191. }
  192. return false;
  193. }
  194. /**
  195. * Get receiving link message
  196. */
  197. public function getRevLink()
  198. {
  199. if(isset($this->_receive['Title'])){
  200. return array(
  201. ' title' => $this->_receive['Title'], //Message title
  202. 'description' => $this->_receive['Description'], //Message description
  203. 'url' => ; $this->_receive['Url'] //Message link
  204. );
  205. }
  206. return false;
  207. }
  208. /**
  209. * Get the receiving event type
  210. * Event types such as: subscribe, unsubscribe, click
  211. */
  212. public function getRevEvent()
  213. {
  214. if(isset ($this->_receive['Event']))
  215. {
  216. return array(
  217. 'event' => strtolower($this->_receive['Event']),
  218. 'key'=> isset ($this->_receive['EventKey']) ? $this->_receive['EventKey'] : ''
  219. );
  220. }
  221. return false;
  222. }
  223. /**
  224. * Set reply text message
  225. * @param string $content
  226. * @param string $openid
  227. */
  228. public function text($content='')
  229. {
  230. $textTpl = "
  231. < ;![CDATA[%s]]>
  232. %s
  233. ";
  234. $this->_reply = sprintf($textTpl,
  235. $this->getRevFrom (),
  236. $this->getRevTo(),
  237. Date::getTimeStamp(),
  238. 'text',
  239. $content
  240. );
  241. return $this;
  242. }
  243. /**
  244. * Set reply music information
  245. * @param string $title
  246. * @param string $desc
  247. * @param string $musicurl
  248. * @param string $hgmusicurl
  249. */
  250. public function music($title, $desc, $musicurl, $hgmusicurl='')
  251. {
  252. $textTpl = '
  253. < /ToUserName>
  254. %s
  255. <![CDATA[%s]]>
  256. ';
  257. //
  258. $this->_reply = sprintf( $textTpl,
  259. $this->getRevFrom(),
  260. $this->getRevTo(),
  261. Date::getTimeStamp(),
  262. 'music',
  263. $title,
  264. $desc,
  265. $musicurl,
  266. $ hgmusicurl
  267. );
  268. return $this;
  269. }
  270. /**
  271. * Reply to text message
  272. * @param array
  273. */
  274. public function news($data)
  275. {
  276. $count = count($data);
  277. $subText = '';
  278. if($count > 0)
  279. {
  280. foreach($data as $v)
  281. {
  282. $tmpText = '
  283. <![CDATA[%s]]></ Title> </li> <li> <Description><![CDATA[%s]]></Description></li> <li> <PicUrl><![CDATA[%s]]></PicUrl></li> <li> <Url> ;<![CDATA[%s]]></Url></li> <li> </item>';</li> <li> </li> <li> $subText .= sprintf(</li> <li> $tmpText, $v['title'], </li> <li> isset($ v['description']) ? $v['description'] : '', </li> <li> isset($v['picUrl']) ? $v['picUrl'] : '', </li> <li> isset($v['url ']) ? $v['url'] : ''</li> <li> );</li> <li> }</li> <li> }</li> <li> </li> <li> $textTpl = '<xml></li> <li> <ToUserName><![CDATA[%s]]></ToUserName></li> <li> <FromUserName><![CDATA[%s]]></FromUserName></li> <li> <CreateTime><![CDATA[%s]]></CreateTime></li> <li> <MsgType><![CDATA[news]]></MsgType></li> <li> <ArticleCount><![CDATA[%d]]></ArticleCount></li> <li> <Articles>%s</Articles></li> <li> </xml>';</li> <li> </li> <li> $this->_reply = sprintf(</li> <li> $textTpl, </li> <li> $this->getRevFrom(), </li> <li> $this->getRevTo(), </li> <li> Date::getTimeStamp(), </li> <li> $count, </li> <li> $subText</li> <li> );</li> <li> return $this;</li> <li> }</li> <li> </li> <li> </li> <li> /**</li> <li> * Reply message </li> <li> * @param array $msg</li> <li> * @param bool $return</li> <li>*/</li> <li> public function reply()</li> <li> {</li> <li> header('Content-Type:text/xml');</li> <li> echo $this->_reply;</li> <li> exit;</li> <li> }</li> <li> </li> <li> </li> <li> /**</li> <li> * Custom menu creation </li> <li> * @param array menu data </li> <li>*/</li> <li> public function createMenu($data)</li> <li> {</li> <li> if(!$this->access_token && !$this->checkAuth()) return false;</li> <li> </li> <li> $result = curlRequest(self::API_URL_PREFIX.self::MENU_CREATE_URL.'access_token='.$this->access_token, $this->jsonEncode($data), 'post');</li> <li> if($result)</li> <li> {</li> <li> $jsonArr = json_decode($result, true);</li> <li> if(!$jsonArr || (isset($jsonArr['errcode']) && $jsonArr['errcode'] > 0)) $this->error($jsonArr);</li> <li> else return true;</li> <li> }</li> <li> </li> <li> return false;</li> <li> }</li> <li> </li> <li> </li> <li> /**</li> <li> *Customized menu query</li> <li>*/</li> <li> public function getMenu()</li> <li> {</li> <li> if(!$this->access_token && !$this->checkAuth()) return false;</li> <li> </li> <li> $result = curlRequest(self::API_URL_PREFIX.self::MENU_GET_URL.'access_token='.$this->access_token);</li> <li> if($result)</li> <li> {</li> <li> $jsonArr = json_decode($result, true);</li> <li> if(!$jsonArr || (isset($jsonArr['errcode']) && $jsonArr['errcode'] > 0)) $this->error($jsonArr);</li> <li> else return $jsonArr;</li> <li> }</li> <li> </li> <li> return false;</li> <li> }</li> <li> </li> <li> </li> <li> /**</li> <li> *Custom menu deleted</li> <li>*/</li> <li> public function deleteMenu()</li> <li> {</li> <li> if(!$this->access_token && !$this->checkAuth()) return false;</li> <li> </li> <li> $result = curlRequest(self::API_URL_PREFIX.self::MENU_DELETE_URL.'access_token='.$this->access_token);</li> <li> if($result)</li> <li> {</li> <li> $jsonArr = json_decode($result, true);</li> <li> if(!$jsonArr || (isset($jsonArr['errcode']) && $jsonArr['errcode'] > 0)) $this->error($jsonArr);</li> <li> else return true;</li> <li> }</li> <li> </li> <li> return false;</li> <li> }</li> <li> </li> <li> </li> <li> /**</li> <li> * Get basic user information </li> <li> * @param string $openid Identification of ordinary users, unique to the current public account </li> <li>*/</li> <li> public function getUserInfo($openid)</li> <li> {</li> <li> if(!$this->access_token && !$this->checkAuth()) return false;</li> <li> </li> <li> $result = curlRequest(self::API_URL_PREFIX.self::USER_INFO_URL.'access_token='.$this->access_token.'&openid='.$openid);</li> <li> if($result)</li> <li> {</li> <li> $jsonArr = json_decode($result, true);</li> <li> if(!$jsonArr || (isset($jsonArr['errcode']) && $jsonArr['errcode'] > 0)) $this->error($jsonArr);</li> <li> else return $jsonArr;</li> <li> }</li> <li> </li> <li> return false;</li> <li> }</li> <li> </li> <li> </li> <li> /**</li> <li> * Get the follower list </li> <li> * @param string $next_openid The first OPENID to pull, if not filled in, it will start pulling from the beginning by default </li> <li>*/</li> <li> public function getUserList($next_openid='')</li> <li> {</li> <li> if(!$this->access_token && !$this->checkAuth()) return false;</li> <li> </li> <li> $result = curlRequest(self::API_URL_PREFIX.self::USER_GET_URL.'access_token='.$this->access_token.'&next_openid='.$next_openid);</li> <li> if($result)</li> <li> {</li> <li> $jsonArr = json_decode($result, true);</li> <li> if(!$jsonArr || (isset($jsonArr['errcode']) && $jsonArr['errcode'] > 0)) $this->error($jsonArr);</li> <li> else return $jsonArr;</li> <li> }</li> <li> </li> <li> return false;</li> <li> }</li> <li> </li> <li> </li> <li> /**</li> <li> * Query grouping </li> <li>*/</li> <li> public function getGroup()</li> <li> {</li> <li> if(!$this->access_token && !$this->checkAuth()) return false;</li> <li> </li> <li> $result = curlRequest(self::API_URL_PREFIX.self::GROUPS_GET_URL.'access_token='.$this->access_token);</li> <li> if($result)</li> <li> {</li> <li> $jsonArr = json_decode($result, true);</li> <li> if(!$jsonArr || (isset($jsonArr['errcode']) && $jsonArr['errcode'] > 0)) $this->error($jsonArr);</li> <li> else return $jsonArr;</li> <li> }</li> <li> </li> <li> return false;</li> <li> }</li> <li> </li> <li> </li> <li> /**</li> <li> * Create a group </li> <li> * @param string $name Group name (within 30 characters) </li> <li>*/</li> <li> public function createGroup($name)</li> <li> {</li> <li> if(!$this->access_token && !$this->checkAuth()) return false;</li> <li> $data = array('group' => array('name' => $name));</li> <li> $result = curlRequest(self::API_URL_PREFIX.self::GROUPS_CREATE_URL.'access_token='.$this->access_token, $this->jsonEncode($data), 'post');</li> <li> if($result)</li> <li> {</li> <li> $jsonArr = json_decode($result, true);</li> <li> if(!$jsonArr || (isset($jsonArr['errcode']) && $jsonArr['errcode'] > 0)) $this->error($jsonArr);</li> <li> else return true;</li> <li> }</li> <li> </li> <li> return false;</li> <li> }</li> <li> </li> <li> </li> <li> /**</li> <li> * Modify the group name </li> <li> * @param int $id Group id, assigned by WeChat </li> <li> * @param string $name Group name (within 30 characters) </li> <li>*/</li> <li> public function updateGroup($id, $name)</li> <li> {</li> <li> if(!$this->access_token && !$this->checkAuth()) return false;</li> <li> </li> <li> $data = array('group' => array('id' => $id, 'name' => $name));</li> <li> $result = curlRequest(self::API_URL_PREFIX.self::GROUPS_UPDATE_URL.'access_token='.$this->access_token, $this->jsonEncode($data), 'post');</li> <li> if($result)</li> <li> {</li> <li> $jsonArr = json_decode($result, true);</li> <li> if(!$jsonArr || (isset($jsonArr['errcode']) && $jsonArr['errcode'] > 0)) $this->error($jsonArr);</li> <li> else return true;</li> <li> }</li> <li> </li> <li> return false;</li> <li> }</li> <li> </li> <li> </li> <li> /**</li> <li> * Mobile user group</li> <li> * </li> <li> * @param string $openid User unique identifier</li> <li> * @param int $to_groupid Group id</li> <li>*/</li> <li> public function updateGroupMembers($openid, $to_groupid)</li> <li> {</li> <li> if(!$this->access_token && !$this->checkAuth()) return false;</li> <li> </li> <li> $data = array('openid' => $openid, 'to_groupid' => $to_groupid);</li> <li> $result = curlRequest(self::API_URL_PREFIX.self::GROUPS_MEMBERS_UPDATE_URL.'access_token='.$this->access_token, $this->jsonEncode($data), 'post');</li> <li> if($result)</li> <li> {</li> <li> $jsonArr = json_decode($result, true);</li> <li> if(!$jsonArr || (isset($jsonArr['errcode']) && $jsonArr['errcode'] > 0)) $this->error($jsonArr);</li> <li> else return true;</li> <li> }</li> <li> </li> <li> return false;</li> <li> }</li> <li> </li> <li> </li> <li> /**</li> <li> * 发送客服消息</li> <li> * 当用户主动发消息给公众号的时候(包括发送信息、点击自定义菜单clike事件、订阅事件、扫描二维码事件、支付成功事件、用户维权),</li> <li> * 微信将会把消息数据推送给开发者,开发者在一段时间内(目前为24小时)可以调用客服消息接口,通过POST一个JSON数据包来发送消息给普通用户,在24小时内不限制发送次数。</li> <li> * 此接口主要用于客服等有人工消息处理环节的功能,方便开发者为用户提供更加优质的服务。</li> <li> * </li> <li> * @param string $touser ordinary user openid</li> <li> */</li> <li> public function sendCustomMessage($touser, $data, $msgType = 'text')</li> <li> {</li> <li> $arr = array();</li> <li> $arr['touser' ] = $touser;</li> <li> $arr['msgtype'] = $msgType;</li> <li> switch ($msgType)</li> <li> {</li> <li> case 'text': // Send text message</li> <li> $arr['text']['content'] = $data; </li> <li> break;</li> <li> </li> <li> case 'image': // Send a picture message </li> <li> $arr['image']['media_id'] = $data;</li> <li> break;</li> <li> </li> <li> case 'voice': // Send a voice message </li> <li> $arr['voice']['media_id'] = $data;</li> <li> break;</li> <li> </li> <li> case 'video': // Send video message </li> <li> $arr['video']['media_id'] = $data[' media_id']; // The media ID of the video sent</li> <li> $arr['video']['thumb_media_id'] = $data['thumb_media_id']; // The media ID of the video thumbnail</li> <li> break;</li> <li> </li> <li> case 'music ': //Send music message</li> <li> $arr['music']['title'] = $data['title'];//Music title</li> <li> $arr['music']['description'] = $data[ 'description'];//Music description</li> <li> $arr['music']['musicurl'] = $data['musicurl'];//Music link</li> <li> $arr['music']['hqmusicurl'] = $ data['hqmusicurl'];// High-quality music link, the wifi environment will give priority to using this link to play music</li> <li> $arr['music']['thumb_media_id'] = $data['title'];// Thumbnail media ID</li> <li> break;</li> <li> </li> <li> case 'news': //Send graphic message</li> <li> $arr['news']['articles'] = $data; // title, description, url, picurl</li> <li> break;</li> <li> } </li> <li> </li> <li> if(!$this->access_token && !$this->checkAuth()) return false;</li> <li> </li> <li> $result = curlRequest(self::API_URL_PREFIX.self::MESSAGE_CUSTOM_SEND_URL.'access_token='.$this-> access_token, $this->jsonEncode($arr), 'post');</li> <li> if($result)</li> <li> {</li> <li> $jsonArr = json_decode($result, true);</li> <li> if(!$jsonArr || (isset($ jsonArr['errcode']) && $jsonArr['errcode'] > 0)) $this->error($jsonArr);</li> <li> else return true;</li> <li> }</li> <li> </li> <li> return false;</li> <li> }</li> <li> </li> <li> </li> <li> </li> <li> / **</li> <li> * Get access_token</li> <li>*/</li> <li> public function checkAuth()</li> <li> {</li> <li> </li> <li> // Get access_token from cache</li> <li> $cache_flag = 'weixin_access_token';</li> <li> $access_token = cache($cache_flag);</li> <li> if($access_token) </li> <li> { </li> <li> $this->access_token = $access_token;</li> <li> return true;</li> <li> }</li> <li> </li> <li> // Request the WeChat server to obtain access_token </li> <li> $result = curlRequest(self::API_URL_PREFIX.self::AUTH_URL.'appid='.$this- >appid.'&secret='.$this->appsecret);</li> <li> if($result)</li> <li> {</li> <li> $jsonArr = json_decode($result, true);</li> <li> if(!$jsonArr || (isset($jsonArr ['errcode']) && $jsonArr['errcode'] > 0))</li> <li> {</li> <li> $this->error($jsonArr);</li> <li> }</li> <li> else</li> <li> {</li> <li> $this->access_token = $jsonArr[ 'access_token'];</li> <li> $expire = isset($jsonArr['expires_in']) ? intval($jsonArr['expires_in'])-100 : 3600;</li> <li> // Save access_token to cache</li> <li> cache($cache_flag, $this->access_token, $expire, Cache::FILE); </li> <li> return true;</li> <li> }</li> <li> }</li> <li> return false;</li> <li> }</li> <li> </li> <li> </li> <li> /**</li> <li> * WeChat oauth login->Step 1: User consent Authorize, get code</li> <li> * Application authorization scope, snsapi_base (the authorization page will not pop up, jump directly, only the user's openid can be obtained), </li> <li> * snsapi_userinfo (the authorization page will pop up, you can get the nickname, gender, and location through openid). Moreover, even if you are not following the user, you can still obtain their information as long as the user authorizes it)</li> <li> * Open the link directly on WeChat, you do not need to fill in this parameter.When doing page 302 redirection, you must bring this parameter</li> <li> * </li> <li> * @param string $redirect_uri The callback link address for redirection after authorization</li> <li> * @param string $scope Application authorization scope 0 is snsapi_base, 1 is snsapi_userinfo</li> <li> * @param string $state will bring the state parameter after redirection. Developers can fill in any parameter value</li> <li> */</li> <li> public function redirectGetOauthCode($redirect_uri, $scope=0, $state='')</li> <li> {</li> <li> $scope = ($scope == 0) ? 'snsapi_base' : 'snsapi_userinfo';</li> <li> $url = self::CONNECT_OAUTH_AUTHORIZE_URL.'appid='.$this->appid.'&redirect_uri='.urlencode($redirect_uri).'&response_type=code&scope= '.$scope.'&state='.$state.'#wechat_redirect';</li> <li> redirect($url);</li> <li> }</li> <li> </li> <li> </li> <li> /**</li> <li> * WeChat oauth login-> Step 2: Exchange the code for webpage authorization access_token</li> <li> * </li> <li> * @param string $code</li> <li>*/</li> <li> public function getSnsAccessToken($code)</li> <li> {</li> <li> $ result = curlRequest(self::SNS_OAUTH_ACCESS_TOKEN_URL.'appid='.$this->appid.'&secret='.$this->appsecret.'&code='.$code.'&grant_type=authorization_code');</li> <li> if ($result)</li> <li> {</li> <li> $jsonArr = json_decode($result, true);</li> <li> if(!$jsonArr || (isset($jsonArr['errcode']) && $jsonArr['errcode'] > 0)) $this->error($jsonArr);</li> <li> else return $jsonArr;</li> <li> }</li> <li> </li> <li> return false;</li> <li> }</li> <li> </li> <li> </li> <li> /**</li> <li> * WeChat oauth login-> Step 3: Refresh access_token (if necessary) </li> <li> * Since access_token has a short validity period, when the access_token times out, you can use refresh_token to refresh, </li> <li> * refresh_token has a longer validity period (7 days , 30 days, 60 days, 90 days), when refresh_token expires, the user needs to re-authorize. </li> <li> * </li> <li> * @param string $refresh_token Fill in the refresh_token parameter obtained through access_token</li> <li>*/</li> <li> public function refershToken($refresh_token)</li> <li> {</li> <li> $result = curlRequest(self::SNS_OAUTH_REFRESH_TOKEN_URL.'appid='.$this->appid.'&grant_type=refresh_token&refresh_token='.$refresh_token);</li> <li> if($result)</li> <li> {</li> <li> $jsonArr = json_decode($result, true) ;</li> <li> if(!$jsonArr || (isset($jsonArr['errcode']) && $jsonArr['errcode'] > 0)) $this->error($jsonArr);</li> <li> else return $jsonArr; </li> <li> }</li> <li> </li> <li> return false;</li> <li> }</li> <li> </li> <li> </li> <li> /**</li> <li> * WeChat oauth login-> Step 4: Pull user information (need to have scope snsapi_userinfo)</li> <li> * If the web page authorization scope is snsapi_userinfo, the developer can now pull user information through access_token and openid. </li> <li> * </li> <li> * @param string $access_token Web page authorization interface call certificate, note: this access_token is different from the basic supported access_token</li> <li> * @param string $openid The unique identifier of the user</li> <li>*/</li> <li> public function getSnsUserInfo($access_token, $openid)</li> <li> {</li> <li> $result = curlRequest(self::SNS_USERINFO_URL.'access_token='.$ access_token.'&openid='.$openid);</li> <li> if($result)</li> <li> {</li> <li> $jsonArr = json_decode($result, true);</li> <li> if(!$jsonArr || (isset($jsonArr['errcode']) && $jsonArr['errcode'] > 0)) $this->error($jsonArr);</li> <li> else return $jsonArr;</li> <li> }</li> <li> </li> <li> return false;</li> <li> }</li> <li> </li> <li> </li> <li> /**</li> <li> * Create a QR code ticket</li> <li> * Each time you create a QR code ticket, you need to provide a parameter (scene_id) set by the developer. The process of creating a QR code ticket for temporary QR codes and permanent QR codes is introduced respectively. </li> <li> * </li> <li> * @param int $scene_id Scene value ID, 32-bit integer for temporary QR code, maximum value is 1000 for permanent QR code</li> <li> * @param int $type QR code type, 0 is temporary, 1 It is permanent</li> <li> * @param int $expire The validity time of this QR code, in seconds. The maximum number does not exceed 1800. </li> <li>*/ </li> <li> public function createQrcode($scene_id, $type=0, $expire=1800)</li> <li> {</li> <li> if(!$this->access_token && !$this->checkAuth()) return false;</li> <li> </li> <li> $data = array ();</li> <li> $data['action_info'] = array('scene' => array('scene_id' => $scene_id));</li> <li> $data['action_name'] = ($type == 0 ? ' QR_SCENE' : 'QR_LIMIT_SCENE');</li> <li> if($type == 0) $data['expire_seconds'] = $expire;</li> <li> </li> <li> $result = curlRequest(self::API_URL_PREFIX.self::QRCODE_CREATE_URL.'access_token='. $this->access_token, $this->jsonEncode($data), 'post');</li> <li> if($result)</li> <li> {</li> <li> $jsonArr = json_decode($result, true);</li> <li> if(!$jsonArr | | (isset($jsonArr['errcode']) && $jsonArr['errcode'] > 0)) $this->error($jsonArr);</li> <li> else return $jsonArr;</li> <li> }</li> <li> </li> <li> return false;</li> <li> }</li> <li> </li> <li> </li> <li> /**</li> <li> * Exchange tickets for QR codes</li> <li> * After obtaining a QR code ticket, developers can exchange the ticket for a QR code image. Please note that this interface can be called without logging in. </li> <li> * Reminder: Remember to UrlEncode TICKET</li> <li> * When the ticket is correct, the http return code is 200, which is a picture and can be displayed or downloaded directly. </li> <li> * HTTP error code 404 will be returned in error situations (such as invalid ticket).</li> <li> * </li> <li> * @param string $ticket</li> <li> */</li> <li> public function getQrcodeUrl($ticket)</li> <li> {</li> <li> return self::SHOW_QRCODE_URL.'ticket='.urlencode($ticket);</li> <li> }</li> <li> </li> <li> </li> <li> /**</li> <li> * Record the error log generated by the interface</li> <li>*/</li> <li> public function error($data)</li> <li> {</li> <li> $this->errCode = $data['errcode'];</li> <li> $this->errMsg = $data['errmsg'];</li> <li> Log::info('WEIXIN API errcode:['.$this->errCode.'] errmsg:['.$this->errMsg.']');</li> <li> }</li> <li> </li> <li> </li> <li> /**</li> <li> * Convert Chinese in the array into json data</li> <li> * @param array $arr</li> <li>*/</li> <li> public function jsonEncode($arr) {</li> <li> $parts = array ();</li> <li> $is_list = false;</li> <li> //Find out if the given array is a numerical array</li> <li> $keys = array_keys ( $arr );</li> <li> $max_length = count ( $arr ) - 1;</li> <li> if (($keys [0] === 0) && ($keys [$max_length] === $max_length )) { //See if the first key is 0 and last key is length - 1</li> <li> $is_list = true;</li> <li> for($i = 0; $i < count ( $keys ); $i ++) { //See if each key correspondes to its position<li> if ($i != $keys [$i]) { //A key fails at position check.<li> $is_list = false; //It is an associative array.<li> break;<li> }<li> }<li> }<li> foreach ( $arr as $key => $value ) {</li> <li> if (is_array ( $value )) { //Custom handling for arrays</li> <li> if ($is_list)</li> <li> $parts [] = $this->jsonEncode ( $value ); /* :RECURSION: */</li> <li> else</li> <li> $parts [] = '"' . $key . '":' . $this->jsonEncode ( $value ); /* :RECURSION: */</li> <li> } else {</li> <li> $str = '';</li> <li> if (! $is_list)</li> <li> $str = '"' . $key . '":';</li> <li> //Custom handling for multiple data types</li> <li> if (is_numeric ( $value ) && $value<2000000000)<li> $str .= $value; //Numbers<li> elseif ($value === false)<li> $str .= 'false'; //The booleans<li> elseif ($value === true)<li> $str .= 'true';<li> else<li> $str .= '"' . addslashes ( $value ) . '"'; //All other things<li> // :TODO: Is there any more datatype we should be in the lookout for? (Object?)<li> $parts [] = $str;<li> }<li> }<li> $json = implode ( ',', $parts );<li> if ($is_list)<li> return '[' . $json . ']'; //Return numerical JSON<li> return '{' . $json . '}'; //Return associative JSON<li> }<li><li> <li> /**<li> * Verify signature <li>*/<li> public function checkSignature()<li> {<li> $signature = HttpRequest::getGet('signature');<li> $timestamp = HttpRequest::getGet('timestamp');<li> $nonce = HttpRequest::getGet('nonce');<li> <li> $token = $this->token;</li> <li> $tmpArr = array($token, $timestamp, $nonce);</li> <li> sort($tmpArr);</li> <li> $tmpStr = implode($tmpArr);</li> <li> $tmpStr = sha1($tmpStr);</li> <li> </li> <li> return ($tmpStr == $signature ? true : false);</li> <li> }</li> <li> </li> <li> </li> <li> /**</li> <li> * Verify whether the token is valid</li> <li>*/</li> <li> public function valid()</li> <li> {</li> <li> if($this->checkSignature()) exit(HttpRequest::getGet('echostr'));</li> <li> }</li> <li> </li> <li>}</li> </ol></div> <em onclick="copycode($('code_ZWw'));">复制代码</em> </div> </td></tr></table> <div id="comment_51454" class="cm"> </div> <div id="post_rate_div_51454"></div> <br><br></div><div class="nphpQianMsg"><div class="clear"></div></div><div class="nphpQianSheng"><span>Statement:</span><div>The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn</div></div></div><div class="nphpSytBox"><span>Previous article:<a class="dBlack" title="WeChat scan login" href="https://m.php.cn/faq/314409.html">WeChat scan login</a></span><span>Next article:<a class="dBlack" title="WeChat scan login" href="https://m.php.cn/faq/314425.html">WeChat scan login</a></span></div><div class="nphpSytBox2"><div class="nphpZbktTitle"><h2>Related articles</h2><em><a href="https://m.php.cn/article.html" class="bBlack"><i>See more</i><b></b></a></em><div class="clear"></div></div><ins class="adsbygoogle" style="display:block" data-ad-format="fluid" data-ad-layout-key="-6t+ed+2i-1n-4w" data-ad-client="ca-pub-5902227090019525" data-ad-slot="8966999616"></ins><script> (adsbygoogle = window.adsbygoogle || []).push({}); </script><ul class="nphpXgwzList"><li><b></b><a href="https://m.php.cn/faq/1.html" title="How to use cURL to implement Get and Post requests in PHP" class="aBlack">How to use cURL to implement Get and Post requests in PHP</a><div class="clear"></div></li><li><b></b><a href="https://m.php.cn/faq/1.html" title="How to use cURL to implement Get and Post requests in PHP" class="aBlack">How to use cURL to implement Get and Post requests in PHP</a><div class="clear"></div></li><li><b></b><a href="https://m.php.cn/faq/1.html" title="How to use cURL to implement Get and Post requests in PHP" class="aBlack">How to use cURL to implement Get and Post requests in PHP</a><div class="clear"></div></li><li><b></b><a href="https://m.php.cn/faq/1.html" title="How to use cURL to implement Get and Post requests in PHP" class="aBlack">How to use cURL to implement Get and Post requests in PHP</a><div class="clear"></div></li><li><b></b><a href="https://m.php.cn/faq/2.html" title="All expression symbols in regular expressions (summary)" class="aBlack">All expression symbols in regular expressions (summary)</a><div class="clear"></div></li></ul></div></div><ins class="adsbygoogle" style="display:block" data-ad-format="autorelaxed" data-ad-client="ca-pub-5902227090019525" data-ad-slot="5027754603"></ins><script> (adsbygoogle = window.adsbygoogle || []).push({}); </script><footer><div class="footer"><div class="footertop"><img src="/static/imghwm/logo.png" alt=""><p>Public welfare online PHP training,Help PHP learners grow quickly!</p></div><div class="footermid"><a href="https://m.php.cn/about/us.html">About us</a><a href="https://m.php.cn/about/disclaimer.html">Disclaimer</a><a href="https://m.php.cn/update/article_0_1.html">Sitemap</a></div><div class="footerbottom"><p> © php.cn All rights reserved </p></div></div></footer><script>isLogin = 0;</script><script type="text/javascript" src="/static/layui/layui.js"></script><script type="text/javascript" src="/static/js/global.js?4.9.47"></script></div><script src="https://vdse.bdstatic.com//search-video.v1.min.js"></script><link rel='stylesheet' id='_main-css' href='/static/css/viewer.min.css' type='text/css' media='all'/><script type='text/javascript' src='/static/js/viewer.min.js?1'></script><script type='text/javascript' src='/static/js/jquery-viewer.min.js'></script><script>jQuery.fn.wait = function (func, times, interval) { var _times = times || -1, //100次 _interval = interval || 20, //20毫秒每次 _self = this, _selector = this.selector, //选择器 _iIntervalID; //定时器id if( this.length ){ //如果已经获取到了,就直接执行函数 func && func.call(this); } else { _iIntervalID = setInterval(function() { if(!_times) { //是0就退出 clearInterval(_iIntervalID); } _times <= 0 || _times--; //如果是正数就 -- _self = $(_selector); //再次选择 if( _self.length ) { //判断是否取到 func && func.call(_self); clearInterval(_iIntervalID); } }, _interval); } return this; } $("table.syntaxhighlighter").wait(function() { $('table.syntaxhighlighter').append("<p class='cnblogs_code_footer'><span class='cnblogs_code_footer_icon'></span></p>"); }); $(document).on("click", ".cnblogs_code_footer",function(){ $(this).parents('table.syntaxhighlighter').css('display','inline-table');$(this).hide(); }); $('.nphpQianCont').viewer({navbar:true,title:false,toolbar:false,movable:false,viewed:function(){$('img').click(function(){$('.viewer-close').trigger('click');});}}); </script></body></html>