Verification is required when developing WeChat. Where can the source code be stored in the official Developer Center. After logging on to the public account, you can see a developer center on the left. Then fill in the corresponding Token and url, and then you can develop it if the verification is successful.
Download WeChat php verification source code in the Developer Center-Developer Documentation-Interface Message-verify message authenticity-pull to the bottom to demonstrate the code in php
The downloaded code is as follows:
<? Php
/**
* Wechat php test
* Update time: 20141008
*/
// Define your token
Define ("TOKEN", "weixin ");
$ WechatObj = new wechatCallbackapiTest ();
$ WechatObj-> valid ();
Class wechatCallbackapiTest
{
Public function valid ()
{
$ EchoStr = $ _ GET ["echostr"];
// Valid signature, option
If ($ this-> checkSignature ()){
Echo $ echoStr;
Exit;
}
}
Public function responseMsg ()
{
// Get post data, May be due to the different environments
$ PostStr = $ GLOBALS ["HTTP_RAW_POST_DATA"];
// Extract post data
If (! Empty ($ postStr )){
$ PostObj = simplexml_load_string ($ postStr, 'simplexmlelement', LIBXML_NOCDATA );
$ FromUsername = $ postObj-> FromUserName;
$ ToUsername = $ postObj-> ToUserName;
$ Keyword = trim ($ postObj-> Content );
$ Time = time ();
$ 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 (! Empty ($ keyword ))
{
$ MsgType = "text ";
$ ContentStr = "Welcome to wechat world! ";
$ ResultStr = sprintf ($ textTpl, $ fromUsername, $ toUsername, $ time, $ msgType, $ contentStr );
Echo $ resultStr;
} Else {
Echo "Input something ...";
}
} Else {
Echo "";
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;
}
}
}
?>
Modify the TOKEN you want and write the same in the Developer Center. During verification, $ wechatObj-> valid (); this section of code cannot be removed. After the verification is successful, we can comment out this section $ wechatObj-> valid (); and then use $ wechatObj-> responseMsg (); for testing.
Note: During development, you need to comment out $ wechatObj-> valid (); otherwise, nothing will be displayed during mobile phone testing.