First, Introduction
The micro-credit public platform can be judged according to the information sent by the user, and then the corresponding reply is given, which has good interactivity. The following will simulate the simple reply function, according to this case, the developer can also basically understand the principle of micro-letter interaction, deeper development.
Second, the analysis of ideas
The user sends over the text message, we can extract the keyword, through the simple if...elseif...else ... Realize.
The key code is as follows:
if ($keyword = = "Hello") {
$contentStr = "Hello";
} ElseIf ($keyword = = "Suzhou") {
$contentStr = "There is heaven, under the Hangzhou and Suzhou";
} else{
$contentStr = "Thank you for your attention" Zhuo Jin Suzhou "micro-signal: Zhuojinsz";
}
If the user sends "Hello", then reply "Hello", if the user sends "Suzhou", then reply "There is paradise, the next has Suzhou and Hangzhou", other information, then reply your welcome word.
Three, complete code
<?php/** * WeChat PHP Test * *//define Your token ("define", "token");
$WECHATOBJ = new Wechatcallbackapitest ();
$WECHATOBJ->responsemsg ();
$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 is due to the different $POSTSTR =
$GLOBALS ["Http_raw_post_data"]; Extract post Data if (!empty ($POSTSTR)) {$POSTOBJ = simplexml_load_string ($postStr, ' Simplexmlelem
Ent ', libxml_nocdata);
$RX _type = Trim ($postObj->msgtype);
Switch ($RX _type) {case "text": $resultStr = $this->handletext ($POSTOBJ);
Break
Case "Event": $resultStr = $this->handleevent ($POSTOBJ);
Break
Default $RESULTSTR = "Unknow msg type:". $RX _type;
Break
Echo $resultStr;
}else {echo "";
Exit
The Public Function Handletext ($POSTOBJ) {$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";
if ($keyword = = "Hello") {$contentStr = "Hello";
}elseif ($keyword = = "Suzhou") {$contentStr = "There is heaven, there are Suzhou and Hangzhou;" }else{$CONTENTSTR = "Thank you for your attention" Zhuo Jin Suzhou Micro-signal:Zhuojinsz ";
$RESULTSTR = sprintf ($TEXTTPL, $fromUsername, $toUsername, $time, $msgType, $CONTENTSTR);
Echo $resultStr;
}else{echo "Input something ...";
The Public Function handleevent ($object) {$contentStr = ""; Switch ($object->event) {case "subscribe": $CONTENTSTR = "Thank you for your concern" Zhuo Jin Suzhou "." \ n "." Micro-signal: Zhuojinsz "." \ n "." Excellent splendid, Suzhou City, we provide you with Suzhou Local life guide, Suzhou Related information inquiries, do the best Suzhou Micro-trust platform. "." \ n "." The current platform features are as follows: "." \ n "." "1" Check the weather, such as input: Suzhou weather "." \ n "." "2" check public transport, such as input: Suzhou bus 178 "." \ n "." "3" translation, such as input: translation I love You "." \ n "." "4" Suzhou information inquiries, such as input: Suzhou Guan Qian Jie "." \ n "."
More content, please look forward to ... ";
Break
Default: $contentStr = "Unknow Event:". $object->event;
Break
$RESULTSTR = $this->responsetext ($object, $CONTENTSTR);
return $resultStr; The Public function responsetext ($object, $content, $flag =0) {$textTpl = "<xml> <TOUSERNAME&G t;<! [cdata[%s]]></tousername> <fromusername><! [Cdata[%s]]></fromuSername> <CreateTime>%s</CreateTime> <msgtype><! [cdata[text]]></msgtype> <content><!
[cdata[%s]]></content> <FuncFlag>%d</FuncFlag> </xml> ";
$RESULTSTR = sprintf ($TEXTTPL, $object->fromusername, $object->tousername, Time (), $content, $flag);
return $resultStr;
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;
}}}?>
Four, test
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.