Welcome to follow my public account "APP daily recommendation" to recommend some wonderful Mobile Phone applications and Internet industry information ~! -- Digress: the reason for this series is that this public platform is the first practical-use thing I developed after programming is removed. I have many experiences here, I dare not speak to anyone, but I am in the same age.
Welcome to follow my public account "APP daily recommendation" to recommend some wonderful Mobile Phone applications and Internet industry information ~ ! -- Digress: the reason for this series is that this public platform is the first practical-use thing I developed after programming is removed. I have many experiences here, I dare not speak to anyone, but I am in the same age.
Welcome to myPublicAccount"Daily APP recommendations"
We recommend some wonderful Mobile Phone applications and Internet industry information ~
Final production effect see: http://blog.csdn.net/c123853648/article/details/11095043
We have made a series of preparations before, and we started coding.
First, we needCodeIn, we added such a line during the last verification.Code
$wechatObj = new wechatCallbackapiTest();$wechatObj->valid();
Now that the verification has been completed, we need to modify it as follows:
$wechatObj = new wechatCallbackapiTest();//$wechatObj->valid();$wechatObj->responseMsg();
Below is an official demo
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 be due to the different environments$postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; // //extract post dataif (!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 = "Welcome to wechat world!"; $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); echo $resultStr; }else{ echo "Input something..."; } }else { echo ""; 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;}}}?>
The values of the first 23 and 61
DevelopmentI didn't pay much attention to it. I will not analyze it here. I will try again later.
Let's start with the responseMsg () method.
The $ postStr variable accepts the information submitted by the user (in xml format), and then loads the XML string into the object by calling the simplexml_load_string () function.
After obtaining the $ postObj variable
In this way, you can obtain various types of data from the user and store the data in the corresponding variables. For details, refer to the message interface guide:Http://mp.weixin.qq.com/wiki/index.php? Title = % E6 % B6 % 88% E6 % 81% AF % E6 % 8E % A5 % E5 % 8F % A3 % E6 % 8C % 87% E5 % 8D % 97
$ TextTpl: this variable is used to store the structure of various reply information types. It can be found in the message interface guide. You can add variables of different information types as needed.
$ ContentStr this variable corresponds to the text content of the reply text information
The sprintf function adds the information to be replied to the "structure" defined by $ textTpl. The springf function is used as follows:
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
InDevelopmentIn the process, it is basically based on [get the content sent by the user]-> [process the content]-> [by writing relevant methods, perform related operations on the content in the database. Choose return database operation result and enter the variable in $ textTpl structure. Then click output]