The creation process requires signature verification, which is described as follows:
After the public platform user submits the information, we will request the filled-in Url in the form of a GET request, and bring Four parameters:
* signature — WeChat encrypted signature
* timestamp — timestamp
* nonce — random number
* echostr — random string
Developers verify the legality of URL access by checking signature. If this GET request returns the echostr parameter content as it is, the access will take effect, otherwise the access will fail. The verification signature will be combined with the token parameter, timestamp parameter and nonce parameter filled in by the developer. The encryption process is:
* Sort the three parameters of token, timestamp and nonce in lexicographic order
* Put the three parameters into lexicographic order. Parameter strings are spliced ??into one string for SHA1 encryption
* The encrypted string obtained by the developer can be compared with the signature to identify that the request originated from WeChat.
Code:
<? $signature = $_GET['signature']; $timestamp = $_GET['timestamp']; $nonce = $_GET['nonce']; $token = TOKEN; $tmpArr = array($token, $timestamp, $nonce); sort($tmpArr); $tmpStr = implode( $tmpArr ); $tmpStr = sha1( $tmpStr ); if( $tmpStr == $signature ){ return $_GET['echostr']; }else{ return false; } ?>
However, WeChat does not use json to transmit data:
So we need to use simplexml_load_string to load XML data as an object, and also find POST The method is not urlencode, so set HTTP_RAW_POST_DATA, and then you can read the data.
ToUserName The WeChat ID of the message receiver, usually the WeChat account of the public platform
FromUserName The WeChat ID of the message sender
CreateTime message creation time
MsgType text message is text
Content message content
<?php $postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; //符合微信的POST規(guī)范 if (!emptyempty($postStr)) { $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); //XML轉(zhuǎn)對(duì)象函數(shù),可能最近這一兩年入行的不太清楚XML函數(shù) //數(shù)據(jù)從對(duì)象取出 $fromUsername = $postObj->FromUserName; $toUsername = $postObj->ToUserName; $CreateTime = $postObj->CreateTime; $MsgType = $postObj->MsgType; $Content = $postObj->Content; $keyword = trim($postObj->Content); //安全Trim $time = time(); //XML數(shù)據(jù)體 $textTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[%s]]></MsgType> <Content><![CDATA[%s]]></Content> <FuncFlag>0</FuncFlag> </xml>"; if(!emptyempty( $keyword )) //如果發(fā)信息來了,不是空白POST,微信規(guī)定立即回復(fù),不用推送. { $msgType = "text"; //定義類型 $contentStr = "Hello World,I am Tater!"; //回復(fù) $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); //記住,Tpl是要載入的 echo $resultStr; //輸出,以便微信抓! } else { echo "What are you say!"; //輸入信息有問題,提示輸入! } } else { echo ""; exit; } ?>
The above is the detailed content of Detailed introduction to WeChat interface development. For more information, please follow other related articles on 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)