Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of access_token and Log developed by Wechat Public platform

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces the example analysis of access_token and log developed by Wechat public platform. It is very detailed and has certain reference value. Interested friends must finish reading it.

1. Access_token

1) two kinds of access_token, web authorization access_token and ordinary access_token

1. Wechat web page authorization is realized through the OAuth3.0 mechanism. After the user is authorized to the official account, the official account can obtain a unique API call credential (web page authorization access_token) for web page authorization, and the access_token can be called after authorization, such as obtaining basic user information.

2. For other Wechat APIs, you need to obtain ordinary access_token calls through the "get access_token" API in basic support. Access_token is the only global ticket of the official account. The access_token is currently valid for 2 hours and needs to be refreshed regularly. Repeated acquisition will invalidate the access_token obtained last time.

2) obtain access_token respectively

1. Web Authorization: click View Web Authorization to get the basic information document of the user, and view this document

You can see that the web page authorization access_token is obtained through code, and this code is obtained through an authorized link on Wechat, and then obtained according to the request in the document. For specific link addresses and parameters, please see the documentation.

/ * create a service that needs to be authenticated by Wechat's OAuth3.0 url * @ param $url service number requires authentication to access url * @ param $scope string snsapi_userinfo | snsapi_base * snsapi_userinfo can be used to obtain user information * snsapi_base can be used to obtain openid * @ param string $state custom status value * here It is agreed that the representative of from_weixin is certified by Wechat. Generally, there is no need to easily change * @ return string to return the authenticated url address * / public function createAuthUrl ($url, $scope = 'snsapi_base', $state =' from_weixin') {$url = strval ($url) $authUrl = 'https://open.weixin.qq.com/connect/oauth3/authorize'; / * there is a big pit here. Please do not disturb the order of param * otherwise the Wechat authentication interface will appear white screen * / $param = array ('appid' = > $this- > appId,' redirect_uri' = > urlencode ($url), 'response_type' = >' code', 'scope' = > $scope,' state' = > $state) $seg = array (); foreach ($param as $k = > $v) {$seg [] = "{$k} = {$v}";} return $authUrl. '?. Join ('&, $seg). '# wechat_redirect';}

2. Normal: click to view to get the access token document, and get it through three parameters.

It should be noted here that the obtained token is timed for 2 hours, so I will save it in MongoDB. First, I will compare whether the timeout has expired from the database. If not, I will get it directly from the database to reduce unnecessary requests.

2. Push log

In the interaction with Wechat, will generate a lot of log information, and development often need to analyze these logs, here I save all the logs in MongoDB. The convenience of MongoDB is that any structure of data can be placed in a document, unlike MySQL to define a good field name, I often debug various structures in a document.

In the entry page of Wechat, that is, the URL (server address) mentioned earlier, it will do the logic of saving logs. Logic includes pushing a message while following, scanning a QR code to follow, clicking on a menu to generate an event, clicking on a menu's hyperlink, and so on.

The log structure is as follows:

1. The signature verification logic is included in the code

2. To obtain the request data through file_get_contents ('php://input') is the following getRawMsg method

3. Insert the push log directly into the MongoDB

4. The SimpleXMLElement object that receives the request information is the following parseMsg method

5. HandleEventMsg is dealing with different situations.

/ * Wechat official account entry * / public function actionPortal () {$weixin = new Weixin (); / / signature verification logic / / if ($weixin- > checkSignature ()) {/ / echo $_ GET ['echostr']; / /} / / exit; / / read the original request data $msg = $weixin- > getRawMsg () / / push log $pushlog = new WeixinPushLog (); $pushlog- > logWeixinPush ($msg); $msgObj = $weixin- > parseMsg ($msg); if ($msgObj = false | |! is_object ($msgObj)) {exit } switch ($msgObj- > MsgType) {case 'event': / / receive event message $this- > handleEventMsg ($msgObj); break; default: / / todo break;}} public function getRawMsg () {return file_get_contents (' php://input') } / * parse the received message * @ param string $msg message body * @ return bool | SimpleXMLElement * / public function parseMsg ($msg =') {if (! $msg | | empty ($msg)) {return false;} $msgObj = simplexml_load_string ($msg, 'SimpleXMLElement', LIBXML_NOCDATA) If ($msgObj = false | |! ($msgObj instanceof\ SimpleXMLElement)) {return false;} return $msgObj;}

6. If you want to push a message, the die method needs to be added.

7. The following code lists only two event situations, one is subscription and the other is click event

8. CreateRawTuWenMsg is stitching XML. Click to view the template message API.

Private function handleEventMsg ($msgObj) {$weixin = new Weixin (); $openId = $msgObj- > FromUserName; $fromUserName = $msgObj- > ToUserName; / / No follow, then push if ($msgObj- > Event = = 'subscribe') {$pushData [' PicUrl'] = 'http://mmbiz.qpic.cn/'; $pushData [' Title'] = 'genetic testing to take you to explore the mysteries of life' $pushData ['Description'] =' Why do different people grow differently in height, weight, skin color and shape? But they are often similar to their own parents?' ; $pushData ['Url'] =' http://mp.weixin.qq.com'; $msg = $weixin- > createRawTuWenMsg ($fromUserName, $openId, array ($pushData)); die ($msg);} elseif ($msgObj- > Event = = 'CLICK') {/ / die ($msg) }} public function createRawTuWenMsg ($fromUserName, $toUserName, $items = array ()) {if (! is_array ($items)) {return';} $count = count ($items); $its ='; foreach ($items as $item) {$its. = ITEMTPL;} $msg = 12345678 {$count} {$its} MSG; return $msg } the above is all the contents of the article "sample Analysis of access_token and logs developed by Wechat Public platform". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report