Micro-credit Public Platform Message Interface (PHP)-Official demo has problems

Source: Internet
Author: User

The case code also runs out of control. I did some research and finally got it done.

First of all:

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

The code is as follows Copy Code

? <?php
Define ("TOKEN", "Weixin");
Define ("MESS", "input point");
$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 = "<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 = 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;
}
}
}
?>

And then:

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

This interface has to be validated first, after success

Then comment out the $wechatobj->valid (); This line, while removing//$WECHATOBJ->responsemsg (), the comment for this line.

The message interface can be used, send a message to try?

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.