WeChat public platform development access_token, log
Mar 01, 2017 am 10:09 AM1. access_token
1) Two kinds of access_token, web page authorization access_token and ordinary access_token
1. WeChat web page authorization is implemented through the OAuth2.0 mechanism. When the user authorizes the official account Finally, the official account can obtain a web page authorization-specific interface call credential (web page authorization access_token). Through the web page authorization access_token, it can perform post-authorization interface calls, such as obtaining basic user information.
2. For other WeChat interfaces, you need to obtain the ordinary access_token call through the "Get access_token" interface in basic support. The access_token is the globally unique ticket for the official account. The validity period of the access_token is currently 2 hours and needs to be refreshed regularly. Repeated acquisition will cause the last access_token to become invalid.
2) Obtain access_token respectively
1, web page authorization: Click to view the web page authorization to obtain the user's basic information document. By viewing this document,
You can see that the code is exchanged for the web page authorization access_token, and this code is obtained through an authorization link on WeChat, and then obtained according to the request in the document. For the specific link address and parameters, please refer to the document.
/** ?????*?創(chuàng)建一個需要通過微信的OAuth2.0認證的服務url ?????*?@param?$url?服務號需要認證訪問的url ?????*?@param?$scope?string?snsapi_userinfo?|?snsapi_base ?????*??????snsapi_userinfo?可以用來獲取用戶信息 ?????*??????snsapi_base?可以用來獲取openid ?????*?@param?string?$state?自定義狀態(tài)值 ?????*??????此處約定為from_weixin代表是從微信認證過來,一般無需輕易變化 ?????*?@return?string?返回認證url地址 ?????*/ ????public?function?createAuthUrl($url,?$scope?=?'snsapi_base',?$state?=?'from_weixin') ????{ ????????$url?=?strval($url); ????????$authUrl?=?'https://open.weixin.qq.com/connect/oauth2/authorize'; ????????/** ?????????*?此處有大坑,請不要打亂param的順序 ?????????*?否則微信認證界面會出現(xiàn)白屏 ?????????*/ ????????$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. Ordinary: Click to view the access token document, which is obtained through three parameters.
It should be noted here that the token obtained is time-sensitive, 2 hours, so I will save it in MongoDB and first compare it with the database to see if it has timed out. , if not, get it directly from the database to reduce unnecessary requests.
2. Push logs
During the interaction with WeChat, a lot of log information will be generated, and it is often necessary to analyze these logs during development , here I save the logs in MongoDB. The convenience of MongoDB is that data of any structure can be placed in a document. Unlike MySQL, which requires well-defined field names, I often put various structures in a document when debugging.
In the entrance page of WeChat, which is the URL (server address) mentioned above, the logic of saving logs will be done here. The logic includes pushing a message when following, scanning the QR code to follow, clicking on a menu to generate an event, clicking on the hyperlink of the menu, etc.
The log structure is as follows:
1. The code includes signature verification logic
2. Through file_get_contents('php: //input') to obtain the request data, which is the getRawMsg method below
3. Insert the push log directly into MongoDB
4. The received request information SimpleXMLElement object is The following parseMsg method
5 and handleEventMsg handle various situations
/** ?????*?微信公眾號入口 ?????*/ ????public?function?actionPortal() ????{ ????????$weixin?=?new?Weixin(); ????????//簽名驗證邏輯 //?????????if($weixin->checkSignature()){ //?????????????echo?$_GET['echostr']; //?????????} //?????????exit; ????????//讀取原始請求數(shù)據(jù) ????????$msg?=?$weixin->getRawMsg(); ???????? ????????//推送日志 ????????$pushlog?=?new?WeixinPushLog(); ????????$pushlog->logWeixinPush($msg); ???????? ????????$msgObj?=?$weixin->parseMsg($msg); ????????if?($msgObj?===?false?||?!is_object($msgObj))?{ ????????????exit; ????????} ????????switch?($msgObj->MsgType)?{ ????????????case?'event'?:?//接收事件消息 ????????????????$this->handleEventMsg($msgObj); ????????????????break; ????????????default?: ????????????????//todo ????????????????break; ????????} ????}
public?function?getRawMsg() ????{ ????????return?file_get_contents('php://input'); ????} ????/** ?????*?解析接收到的消息 ?????*?@param?string?$msg?消息體 ?????*?@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 messages, the die method must be added
7. The following code only lists two event situations, one is subscription and the other is Click event
8, createRawTuWenMsg is splicing XML, click to view the template message interface.
private?function?handleEventMsg($msgObj) ????{ ????????$weixin?=?new?Weixin(); ????????$openId?=?$msgObj->FromUserName; ????????$fromUserName?=?$msgObj->ToUserName; ????????//未關注,關注后推送 ????????if?($msgObj->Event?==?'subscribe')?{ ????????????$pushData['PicUrl']?=?'http://mmbiz.qpic.cn/'; ????????????$pushData['Title']?=?'基因檢測,帶你一起探索生命的奧妙?'; ????????????$pushData['Description']?=?'為什么不同人在身高、體重、膚色和形狀上長得不一樣?但是往往又和自己的父母相似?'; ????????????$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 <item> <Title><![CDATA[{$item['Title']}]]></Title> <Description><![CDATA[{$item['Description']}]]></Description> <PicUrl><![CDATA[{$item['PicUrl']}]]></PicUrl> <Url><![CDATA[{$item['Url']}]]></Url> </item> ITEMTPL; ????????} ???? ????????$msg?=?<<<MSG <xml> <ToUserName><![CDATA[{$toUserName}]]></ToUserName> <FromUserName><![CDATA[{$fromUserName}]]></FromUserName> <CreateTime>12345678</CreateTime> <MsgType><![CDATA[news]]></MsgType> <ArticleCount>{$count}</ArticleCount> <Articles> {$its} </Articles> </xml> MSG; ????return?$msg; ????}
demo download:
github address: https://github.com/pwstrick/weixin_demo
CSDN address: http://download.csdn.net/detail/loneleaf1/9045731
For more articles related to WeChat public platform development access_token and logs, please pay attention to the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

PHP is an open source scripting language that is widely used in web development and server-side programming, especially in WeChat development. Today, more and more companies and developers are starting to use PHP for WeChat development because it has become a truly easy-to-learn and easy-to-use development language. In WeChat development, message encryption and decryption are a very important issue because they involve data security. For messages without encryption and decryption methods, hackers can easily obtain the data, posing a threat to users.

In the development of WeChat public accounts, the voting function is often used. The voting function is a great way for users to quickly participate in interactions, and it is also an important tool for holding events and surveying opinions. This article will introduce you how to use PHP to implement WeChat voting function. Obtain the authorization of the WeChat official account. First, you need to obtain the authorization of the WeChat official account. On the WeChat public platform, you need to configure the API address of the WeChat public account, the official account, and the token corresponding to the public account. In the process of our development using PHP language, we need to use the PH officially provided by WeChat

With the popularity of WeChat, more and more companies are beginning to use it as a marketing tool. The WeChat group messaging function is one of the important means for enterprises to conduct WeChat marketing. However, if you only rely on manual sending, it is an extremely time-consuming and laborious task for marketers. Therefore, it is particularly important to develop a WeChat mass messaging tool. This article will introduce how to use PHP to develop WeChat mass messaging tools. 1. Preparation work To develop WeChat mass messaging tools, we need to master the following technical points: Basic knowledge of PHP WeChat public platform development Development tools: Sub

WeChat is currently one of the social platforms with the largest user base in the world. With the popularity of mobile Internet, more and more companies are beginning to realize the importance of WeChat marketing. When conducting WeChat marketing, customer service is a crucial part. In order to better manage the customer service chat window, we can use PHP language for WeChat development. 1. Introduction to PHP WeChat development PHP is an open source server-side scripting language that is widely used in the field of Web development. Combined with the development interface provided by WeChat public platform, we can use PHP language to conduct WeChat

In the development of WeChat public accounts, user tag management is a very important function, which allows developers to better understand and manage their users. This article will introduce how to use PHP to implement the WeChat user tag management function. 1. Obtain the openid of the WeChat user. Before using the WeChat user tag management function, we first need to obtain the user's openid. In the development of WeChat public accounts, it is a common practice to obtain openid through user authorization. After the user authorization is completed, we can obtain the user through the following code

As WeChat becomes an increasingly important communication tool in people's lives, its agile messaging function is quickly favored by a large number of enterprises and individuals. For enterprises, developing WeChat into a marketing platform has become a trend, and the importance of WeChat development has gradually become more prominent. Among them, the group sending function is even more widely used. So, as a PHP programmer, how to implement group message sending records? The following will give you a brief introduction. 1. Understand the development knowledge related to WeChat public accounts. Before understanding how to implement group message sending records, I

How to use PHP to develop WeChat public accounts WeChat public accounts have become an important channel for promotion and interaction for many companies, and PHP, as a commonly used Web language, can also be used to develop WeChat public accounts. This article will introduce the specific steps to use PHP to develop WeChat public accounts. Step 1: Obtain the developer account of the WeChat official account. Before starting the development of the WeChat official account, you need to apply for a developer account of the WeChat official account. For the specific registration process, please refer to the official website of WeChat public platform

With the development of the Internet and mobile smart devices, WeChat has become an indispensable part of the social and marketing fields. In this increasingly digital era, how to use PHP for WeChat development has become the focus of many developers. This article mainly introduces the relevant knowledge points on how to use PHP for WeChat development, as well as some of the tips and precautions. 1. Development environment preparation Before developing WeChat, you first need to prepare the corresponding development environment. Specifically, you need to install the PHP operating environment and the WeChat public platform
