This example for you to share the PHP micro-letter Interface class for your reference, the specific content as follows
<?php/** * WeChat PHP Test *///define your token//define token secret key define ("token", "Weixin");
Instantiation of the micro-object $WECHATOBJ = new Wechatcallbackapitest ();
After the successful validation of the annotation valid method//$WECHATOBJ->valid ();
Turn on automatic reply function $wechatObj->responsemsg (); Defines class file class Wechatcallbackapitest {//Implementation valid authentication method: Implement docking micro-trust public platform common function valid () {//Accept random string $echoStr
= $_get["Echostr"]; Valid signature, option//user digital signature Verification if ($this->checksignature ()) {//If successful, returns the received random string echo $echo
STR;
Exits exit; }///define automatic Reply functionality public function responsemsg () {//get post data, May is due to the different environments/
/accept the XML data sent over by the client $postStr = $GLOBALS ["Http_raw_post_data"]; Extract post data//determine if the XML data is an empty if (!empty ($POSTSTR)) {/* Libxml_disable_entity_loader is to prevent XML EXternal Entity injection, the best way is to check the validity of XML by yourself/libxml_disable_e
Ntity_loader (TRUE); XML parsing through SimpleXML $POSTOBJ = simplexml_load_string ($postStr, ' simplexmlelement ', libxml_nocdata);
Accept micro-letter mobile Phone $fromUsername = $POSTOBJ->fromusername;
Micro-Credit Public Platform $toUsername = $POSTOBJ->tousername;
Accept the user sent the keyword $keyword = Trim ($postObj->content);
1. Accept the user message type $msgType = $POSTOBJ-> msgtype;
Timestamp $time = time (); Text Send template $textTpl = "<xml> <tousername><! [cdata[%s]]></tousername> <fromusername><! [cdata[%s]]></fromusername> <CreateTime>%s</CreateTime> <MSGTYPE>&L t;! [cdata[%s]]></msgtype> <content><!
[cdata[%s]]></content> <FuncFlag>0</FuncFlag> </xml> "; If the user sends a text type file, executes theNext if ($msgType = = ' text ') {if (!empty ($keyword)) {/* This is an instance//if text is sent
Information $msgType = "text";
Reply Content if ($keyword = = "Linan") {$contentStr = "What do you Call me";
}else{$contentStr = "What do you Call me"; //Format XML template, the parameter is one by one corresponding to the template above. Fromusername and head username are the opposite, write only the $resultStr with%s = sprintf ($TEXTTPL, $
Fromusername, $toUsername, $time, $msgType, $CONTENTSTR);
Returns the XML information to the client echo $resultStr; * * if ($keyword = = "?" | | $keyword = = "?
") {$msgType =" text ";
$CONTENTSTR = "1. Special service number \N2. Communication Service Number";
$RESULTSTR = sprintf ($TEXTTPL, $fromUsername, $toUsername, $time, $msgType, $CONTENTSTR);
Echo $resultStr;
}elseif ($keyword = = 1) {$msgType = "text";
$CONTENTSTR = "1. Bandit Police: 110\n2. Fire: 119\n3. First aid: 120"; $resultStR = sprintf ($TEXTTPL, $fromUsername, $toUsername, $time, $msgType, $CONTENTSTR);
Echo $resultStr;
}elseif ($keyword = = 2) {$msgType = "text";
$CONTENTSTR = "1. China Mobile: 10086\n2. Unicom: 10010";
$RESULTSTR = sprintf ($TEXTTPL, $fromUsername, $toUsername, $time, $msgType, $CONTENTSTR);
Echo $resultStr;
}else{echo "cannot but speak";
}//////////////////////////////////////////////////////////////////////////////////////Accept picture information
if ($msgType = = "image") {//If the text message is sent $msgType = "text";
The reply content $contentStr = "You send is the picture file";
format string $resultStr = sprintf ($TEXTTPL, $fromUsername, $toUsername, $time, $msgType, $CONTENTSTR);
Returns the XML information to the client echo $resultStr; }////////////////////////////////////////////////////////////////////////////////////if ($msgType= = "Voice") {//If the text message is sent $msgType = "text";
Reply Content $contentStr = "You are sending a voice file";
format string $resultStr = sprintf ($TEXTTPL, $fromUsername, $toUsername, $time, $msgType, $CONTENTSTR);
Returns the XML information to the client echo $resultStr; ////////////////////////////////////////////////////////////////////////////////////if ($msgType = = "vid
EO ") {//If the text message is sent $msgType =" text ";
The reply content $contentStr = "You send is the video file";
format string $resultStr = sprintf ($TEXTTPL, $fromUsername, $toUsername, $time, $msgType, $CONTENTSTR);
Returns the XML information to the client echo $resultStr; ////////////////////////////////////////////////////////////////////////////////////if ($msgType = = "Sho
Rtvideo ") {//If the text message is sent $msgType =" text ";
Reply Content $contentStr = "You send is small video file"; Panesprintf string $resultStr = ($TEXTTPL, $fromUsername, $toUsername, $time, $msgType, $CONTENTSTR);
Returns the XML information to the client echo $resultStr; ////////////////////////////////////////////////////////////////////////////////////if ($msgType = = "Loc
ation ") {//If the text message is sent $msgType =" text ";
Reply Content $contentStr = "You are sending the location file";
format string $resultStr = sprintf ($TEXTTPL, $fromUsername, $toUsername, $time, $msgType, $CONTENTSTR);
Returns the XML information to the client echo $resultStr; ////////////////////////////////////////////////////////////////////////////////////if ($msgType = = "Lin
K ") {//If the text message is sent $msgType =" text ";
The reply content $contentStr = "You send is the connection file";
format string $resultStr = sprintf ($TEXTTPL, $fromUsername, $toUsername, $time, $msgType, $CONTENTSTR);
Returning XML information to the client Echo $resultStr; }///////////////////////////////////////////////////////////////////////////////////////Judge user sent
Whether the keyword is an empty if (!empty ($keyword)) {//If the text message is sent $msgType = "text";
Reply Content $contentStr = "Everybody is good, I am Hero";
format string $resultStr = sprintf ($TEXTTPL, $fromUsername, $toUsername, $time, $msgType, $CONTENTSTR);
Returns the XML information to the client echo $resultStr;
}else{echo "Input something ...";
} */}else {echo "";
Exit The Private Function checksignature () {//You must define TOKEN by yourself//judge whether or not to define TOKEN, if not, throw a different
Constant if (!defined ("TOKEN")) {throw new Exception (' TOKEN is not defined! '); $signature = $_get["signature"];//accept the micro-letter encryption Signature $timestamp = $_get["timestamp"];//accept timestamp $nonce = $_get["Non" Ce "];//accepts the random number $token = token;//assigns token constant value to $tokeN//Assemble the relevant parameters into a group $TMPARR = Array ($token, $timestamp, $nonce);
Use sort_string the rule//sorted sort ($TMPARR, sort_string);
Converts the sorted array to a string $tmpStr = Implode ($TMPARR);
Encrypt $tmpStr = SHA1 ($TMPSTR) by hashing algorithm;
Compare with encrypted signature if ($tmpStr = = $signature) {//Same returns true return true;
}else{///different returns false return false; }}}?>
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.