Verification steps for php WeChat public platform development

Source: Internet
Author: User


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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.