WeChat public platform message interface (PHP)-official Demo problematic-PHP source code

Source: Internet
Author: User
Public platform message interface (PHP)-the official Demo has a problem. The following is an example of how to handle it. Public platform message interface (PHP)-the official Demo has a problem. The following is an example of how to handle it.

Script ec (2); script

The case code cannot run. I did some research and finally got it done.

First:

Upload an interface file on your server, such as the http://www.xxx.com/weixin.php content as follows:

The Code is as follows:

? Define ("TOKEN", "weixin ");
Define ("MESS", "enter something ");
$ WechatObj = new wechatCallbackapiTest ();
$ WechatObj-> valid ();
// $ WechatObj-> responseMsg ();
Class wechatCallbackapiTest
{
Public function valid ()
{
$ EchoStr = $ _ GET ["echostr"];
If ($ this-> checkSignature ()){
Echo $ echoStr;
Exit;
}
}
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 (! Empty ($ keyword ))
{
$ MsgType = "text ";
$ ContentStr = MESS;
$ ResultStr = sprintf ($ textTpl, $ fromUsername, $ toUsername, $ time, $ msgType, $ contentStr );
Echo $ resultStr;
} Else {
Echo MESS;
}
} Else {
Echo MESS;
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;
}
}
}
?>

Then:

Set the reply interface and fill in the URL and Token (must be consistent with the Token defined above)

This interface must be verified first.

Comment out the $ wechatObj-> valid (); line, and remove the // $ wechatObj-> responseMsg (); line comment.

The message interface can be used. Try sending a message?

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.