<?phpnamespace app\index\controller;use think\Controller;class Index extends controller{ public function index(){ //獲得參數 signature nonce token timestamp echostr $nonce? ? ?= $_GET['nonce']; $token? ? ?= 'imooc'; $timestamp = $_GET['timestamp']; $echostr? ?= $_GET['echostr']; $signature = $_GET['signature']; //形成數組,然后按字典序排序 $array = array(); $array = array($nonce, $timestamp, $token); sort($array); //拼接成字符串,sha1加密 ,然后與signature進行校驗 $str = sha1( implode( $array ) ); if( $str? == $signature && $echostr ){ //第一次接入weixin api接口的時候 echo? $echostr; exit; }else{ $this->reponseMsg(); } } public function reponseMsg(){ //1.獲取到微信推送過來post數據(xml格式) $postArr = $GLOBALS['HTTP_RAW_POST_DATA']; //2.處理消息類型,并設置回復類型和內容 /*<xml><ToUserName><![CDATA[toUser]]></ToUserName><FromUserName><![CDATA[FromUser]]></FromUserName><CreateTime>123456789</CreateTime><MsgType><![CDATA[event]]></MsgType><Event><![CDATA[subscribe]]></Event></xml>*/ $postObj = simplexml_load_string( $postArr ); //$postObj->ToUserName = ''; //$postObj->FromUserName = ''; //$postObj->CreateTime = ''; //$postObj->MsgType = ''; //$postObj->Event = ''; // gh_e79a177814ed //判斷該數據包是否是訂閱的事件推送 if( strtolower( $postObj->MsgType) == 'event'){ //如果是關注 subscribe 事件 if( strtolower($postObj->Event == 'subscribe') ){ //回復用戶消息(純文本格式) $toUser? ?= $postObj->FromUserName; $fromUser = $postObj->ToUserName; $time? ? ?= time(); $msgType? =? 'text'; $content? = '歡迎關注我們的微信公眾賬號'.$postObj->FromUserName.'-'.$postObj->ToUserName; $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); echo $info;????????}????}}跪求各位大神
- 2 回答
- 0 關注
- 3081 瀏覽
添加回答
舉報
0/150
提交
取消