This article describes the PHP version of the micro-letter custom reply function. Share to everyone for your reference, specific as follows:
After configuring the server, you can use PHP to achieve automatic reply.
Code in the index.php
<?php define ("TOKEN", "Weixin"); $wechatObj = new Wechatcallbackapitest (); if (Isset ($ _get[' Echostr ']) {$WECHATOBJ->valid ();} else{$wechatObj->responsemsg (), class Wechatcallbackapitest {public function valid () {$echoStr = $_get["
Echostr "];
if ($this->checksignature ()) {header (' content-type:text ');
Echo $echoStr;
Exit
The Private Function checksignature () {$signature = $_get["signature"];
$timestamp = $_get["timestamp"];
$nonce = $_get["nonce"];
$token = token;
$TMPARR = Array ($token, $timestamp, $nonce);
Sort ($TMPARR, sort_string);
$TMPSTR = implode ($TMPARR);
$TMPSTR = SHA1 ($TMPSTR);
if ($tmpStr = = $signature) {return true;
}else{return false;
The Public Function responsemsg () {$postStr = $GLOBALS ["Http_raw_post_data"]; if (!empty ($POSTSTR)) {$POSTOBJ = simplexml_load_string ($postStr, ' simplexmlelement ', LIBXml_nocdata);
Get Data $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 ($keyword = = "?" | | $keyword = = "?
")//Get user information {$msgType =" text "; $CONTENTSTR = Date ("Y-m-d h:i:s", Time ());
The content of the reply $resultStr = sprintf ($TEXTTPL, $fromUsername, $toUsername, $time, $msgType, $CONTENTSTR);
Echo $resultStr;
}}else{echo "";
Exit }}?>
Effect:
When the user enters? Or, you'll get the current time.
For more information on PHP related content readers can view the site topics: "PHP micro-credit Development Skills summary", "PHP coding and transcoding Operation skills Summary", "PHP Network Programming Skills Summary", "PHP basic Grammar Introductory Course", "PHP string (String) Usage Summary", "php+ MySQL Database operations Introduction tutorial and PHP Common database operation Skills Summary
I hope this article will help you with the PHP program design.