Solution to Token verification failure or URL request timeout in WeChat development-PHP source code

Source: Internet
Author: User
A feature recently used for development is actually a very simple user input, and then the database is automatically searched for and a data reply. This is not a big problem with the official team, however, I have made a lot of effort in Token verification failure. I will give you an analysis on the Solution Below. A feature recently used for development is actually a very simple user input, and then the database is automatically searched for and a data reply. This is not a big problem with the official team, however, I have made a lot of effort in Token verification failure. I will give you an analysis on the Solution Below.

Script ec (2); script

1. Failed Token Verification

This is to check the configuration file, the most basic is

Define ("TOKEN", "weixin"); weixin is the ID of your development background


2. request URL timeout

There is no way to submit it several times. This is also because the server has installed software such as dongle to intercept the IP address. You can check it.

3. Download a wechatCallbackapiTest class and perform the following operations.

The Code is as follows:

Define ("TOKEN", "weixin ");
$ WechatObj = new wechatCallbackapiTest ();
If (isset ($ _ GET ['echostr']) {
$ WechatObj-> valid ();
} Else {
$ WechatObj-> responseMsg ();
}

The code for the wechatCallbackapiTest class is as follows:

The Code is as follows:

Class wechatCallbackapiTest
{
Public function valid ()
{
$ 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 );
$ TmpStr = implode ($ tmpArr );
$ TmpStr = sha1 ($ tmpStr );

If ($ tmpStr = $ signature ){
Return true;
} Else {
Return false;
}
}

Public function responseMsg ()
{
$ PostStr = $ GLOBALS ["HTTP_RAW_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 ="
%s
%s
% S
%s
%s
0
";
If ($ keyword! = "" |! Empty ($ keyword ))
{
MsgType = "text ";
// $ ContentStr. = date ("Y-m-d H: I: s", time ());
$ ResultStr = sprintf ($ textTpl, $ fromUsername, $ toUsername, $ time, $ msgType, $ contentStr );
Echo $ resultStr;
}
} Else {
Echo "";
Exit;
}
}
}

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.