<?php namespace Home\Controller; use Think\Controller; class IndexController extends Controller { public function index(){ //1,将timestamp,nonce,token按照字典排序 $timestamp = $_GET['timestamp']; $nonce = $_GET['nonce']; $token = 'weixin'; $signature = $_GET['signature']; $echostr = $_GET['echostr']; $array = array($timestamp,$nonce,$token); sort($array); //2,将排序后的三个参数进行拼接后用sha1加密 $tmpstr = implode('',$array);//join $tmpstr = sha1($tmpstr); //3,将机密后的字符串与signature进行对比,判断请求是否来自微信 if($tmpstr == $signature && $echostr){ //第一次接入weiixn接口的时候 echo $_GET['echostr']; exit; }else{ $this->reponseMsg(); } } //接收事件推送消息 public function reponseMsg(){ //1.获取到微信推送过来的post数据(xml格式) $postArr = $GLOBALS['HTTP_RAW_POST_DATA']; //2.处理消息类型,并设置回复的类型和内容 $postObj = simplexml_load_string($postArr); // $postObj->ToUserName = ''; // $postObj->FromUserName = ''; // $postObj->CreateTime = ''; // $postObj->Event = ''; //判1 ``````````` // <xml> // <ToUserName>< ![CDATA[toUser] ]></ToUserName> // <FromUserName>< ![CDATA[FromUser] ]></FromUserName> // <CreateTime>123456789</CreateTime> // <MsgType>< ![CDATA[event] ]></MsgType><Event> // < ![CDATA[subscribe] ]></Event> // </xml> if(strtolower($postObj->MsgType) == 'event'){ //如果是关注subscribe事件 if(strtolower($postObj->Event == 'subscribe')){ //如果是取消关注事件 //回复用户信息 $toUser = $postObj->FromUserName; $fromUser = $postObj->ToUserName; $time = time(); $msgType = 'text'; $content = '欢迎关注此微信公众号,用户微信ID:'.$postObj->FromUserName."微信平台ID:".$fromUser; $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; } } } }
微信开发之二:微信绑定后自动回复和获取用户ID与微信平台ID
猜你喜欢
转载自blog.csdn.net/weixin_36171533/article/details/80502539
今日推荐
周排行