<?php /** * wechat php test */ //define your token define("TOKEN", "weixin"); $wechatObj = new wechatCallbackapiTest(); $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 be due to the different environments $postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; //extract 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 = "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, SORT_STRING); $tmpStr = implode( $tmpArr ); $tmpStr = sha1( $tmpStr ); if( $tmpStr == $signature ){ return true; }else{ return false; } } } ?>
3、首先看13行的valid()函数:在15行从微信后台获取一耳光随机字符赋予变量 $echoStr,18-20行访问61行的checkSignature()函数的签名验证方法,如果签名一致,输出变量$echoStr,完整验证配置接口的操作。
private function checkSignature() { //获取GET参数 $signature = $_GET["signature"]; $timestamp = $_GET["timestamp"]; $nonce = $_GET["nonce"]; $token = TOKEN; //把nonce、timestamp、TOKEN组装到数组里并做排序 $tmpArr = array($token, $timestamp, $nonce); sort($tmpArr, SORT_STRING);//排序 //将数组中的元素合成字符串 $tmpStr = implode( $tmpArr ); //sha1加密 $tmpStr = sha1( $tmpStr ); //判断加密后的字符串是否和signature相等 if( $tmpStr == $signature ){ return true; }else{ return false; } }
public function responseMsg() { //这里获取用户发来的数据,并保存到$postStr变量中,由于微信后台发送的数据是XML格式的数据, //所以需要$GLOBALS["HTTP_RAW_POST_DATA"] 方法来获取。 $postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; //如果用户端发来的数据不为空,则执行下面这段代码 if (!empty($postStr)){ //用simplexml_load_string解析XML数据,并放入$postObj变量中 $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); $fromUsername = $postObj->FromUserName;//获得用户名OpenID, $toUsername = $postObj->ToUserName;//获得你的微信公众账号ID $keyword = trim($postObj->Content);//获得用户发给你的文本内容,trim是去掉文本中的空格 $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";//设置回复消息的类型为text类型,即文本类型 $contentStr = "Welcome to wechat world!";//回复的内容放到$contentStr变量中 //下面这行是将用户ID,我们微信公众账号ID,系统时间,回复消息类型,回复内容 打包到$textTpl变量中, $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); echo $resultStr;//输出回复的信息,即发送微信给微信后台,后台再将消息发给用户。 }else{ echo "Input something..."; } }else { echo ""; exit; } }