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?