<?phpnamespace?app\index\controller;class?Index{
????public?function?index()????{????????
????//1.將timestamp,nonce,toke按字典順序排序??
????$timestamp?=?$_GET['timestamp'];??
????$nonce?=?$_GET['nonce'];??
????$token?=?'weixin';??
????$signature?=?$_GET['signature'];??
????$array?=?array($timestamp,$nonce,$token);??
????//2.將排序后的三個(gè)參數(shù)拼接之后用sha1加密??
????$tmpstr?=?implode('',$array);??
????$tmpstr?=?sha1($tmpstr);??
????//3.將加密后的字符串與signature進(jìn)行對(duì)比,判斷該請(qǐng)求是否來自微信??
????if($tmpstr?==?$signature?&&?$_GET['echostr']){ ????
????header('content-type:text'); ??????
????//?第一次接入微信API接口 ????
????echo?$_GET['echostr'];?? ????
????exit;??
????}?else{
???? $this->responseMsg(); }????}????
???? /*接受事件推送并回復(fù)*/????
???? public?function?responseMsg()????{
???? $postArr?=?file_get_contents("php://input"); ????????
???? libxml_disable_entity_loader(true); ????????
???? $postObj?=?simplexml_load_string($postArr,?'SimpleXMLElement',?LIBXML_NOCDATA);
???? //?判斷該數(shù)據(jù)包是否是訂閱的時(shí)間推送
???? if(strtolower($postObj->MsgType)?==?'event'){
???? //?如果是關(guān)注subscribe事件
???? if(strtolower($postObj->Event)?==?'subscribe'){
???? //?回復(fù)用戶信息
???? $toUser?=?$postObj->FromUserName;
???? $fromUser?=?$postObj->ToUserName;
???? $time?=?time();
???? $Msgtype?=?'text';
???? //?$Content?=?'相信自己,你的選擇是對(duì)的。';
???? $Content?=?'相信自己,你的選擇是對(duì)的';
???? $template?=?"<xml><ToUserName><![CDATA[%s]]></ToUserName><FromUserName><![CDATA[%s]]></FromUserName><CreateTime>%s</CreateTime><MsgType><![CDATA[%s]]></MsgType><Content><![CDATA[%s]]></Content></xml>";
???? $info?=?sprintf($template,$toUser,$fromUser,$time,$Msgtype,$Content);
???? //后面的值將會(huì)按順序填到$template里面的%s
???? echo?$info; } }????}}
2018-08-15
少了個(gè)sort($array);加上測(cè)一下