Home  >  Article  >  WeChat Applet  >  Detailed explanation of WeChat applet message push PHP server verification example

Detailed explanation of WeChat applet message push PHP server verification example

巴扎黑
巴扎黑Original
2017-04-01 15:44:521469browse

This article mainly introduces the relevant information on the detailed explanation of the PHP server verification example of the WeChat applet message push. Friends in need can refer to

Detailed explanation of the PHP server verification example of the WeChat applet message push

WeChat documentation (there is an "Access Guide" at the bottom): https://mp.weixin.qq.com/debug/wxadoc/dev/api/custommsg/callback_help.html

Settings page ("Settings" >> "Development Settings"):

https://mp.weixin.qq.com/wxopen/initprofile?action=home&lang=zh_CN

1. Set the server domain name

For example: https://hosts.com

Note the difference between http and https protocols.

2. Set up message push

2.1 Add the server interface test.php to your server. The content of the test.php interface is mainly to verify the message through token Whether it is sent from WeChat, the code refers to the official example:


define("TOKEN","xxxxx");/ 后台填写的token
$wechatObj = new wechatAPI();
$wechatObj->isValid();
class wechatAPI
{
public function isValid()//验证微信接口,如果确认是微信就返回它传来的echostr参数
{
 $echoStr = $_GET["echostr"];
 if ($this->checkSignature()) {
 echo $echoStr;
 exit;
 }
}
private function checkSignature() //官方的验证函数
{
 $signature = $_GET["signature"];
 $timestamp = $_GET["timestamp"];
 $nonce = $_GET["nonce"];
 $token = TOKEN;
 $tmpArr = array($token, $timestamp, $nonce);
 sort($tmpArr, SORT_STRING);
 $tmpStr = implode( $tmpArr );
 $tmpStr = sha1( $tmpStr );
 if( $tmpStr == $signature ){
 return true;
 }else{
 return false;
 }
}
};

2.2 Set up the background message push related information of the mini program

URL (server address): https://hosts.com/xx/test.php

Token: any string that conforms to the specification, such as "xxxxx"

defined above

EncodingAESKey (message encryption key): (randomly generated, just save it yourself, this is only used for encryption and decryption)

Message encryption method: select plaintext for the time being, no need to consider encryption and decryption question.

Data format: Select according to needs.

Submit, if there are no problems, it will be successful. (If there is a problem you can use fake data to pass the browser test)

The above is the detailed content of Detailed explanation of WeChat applet message push PHP server verification example. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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