叶子的思念
2018-05-25 19:28
<?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.将排序后的三个参数拼接之后用sha1加密 $tmpstr = implode('',$array); $tmpstr = sha1($tmpstr); //3.将加密后的字符串与signature进行对比,判断该请求是否来自微信 if($tmpstr == $signature && $_GET['echostr']){ header('content-type:text'); // 第一次接入微信API接口 echo $_GET['echostr']; exit; } else{ $this->responseMsg(); } } /*接受事件推送并回复*/ public function responseMsg() { $postArr = file_get_contents("php://input"); libxml_disable_entity_loader(true); $postObj = simplexml_load_string($postArr, 'SimpleXMLElement', LIBXML_NOCDATA); // 判断该数据包是否是订阅的时间推送 if(strtolower($postObj->MsgType) == 'event'){ // 如果是关注subscribe事件 if(strtolower($postObj->Event) == 'subscribe'){ // 回复用户信息 $toUser = $postObj->FromUserName; $fromUser = $postObj->ToUserName; $time = time(); $Msgtype = 'text'; // $Content = '相信自己,你的选择是对的。'; $Content = '相信自己,你的选择是对的'; $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); //后面的值将会按顺序填到$template里面的%s echo $info; } } }}
少了个sort($array);加上测一下
PHP实现微信公众平台开发—提升篇
64948 学习 · 371 问题
相似问题