求助,關注后沒有消息推送?發(fā)文本后發(fā)出“該公眾號提供的服務出現(xiàn)故障,請稍后再試”
代碼如下,應該跟視頻差不多,不清楚,看了挺多解釋也不是很明白
<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
? ?public function index(){
? ? ? ?//獲得參數(shù) signature nonce token timestamp echostr
? ? ? ?$nonce ? ? = $_GET['nonce'];
? ? ? ?$token ? ? = 'imooc';
? ? ? ?$timestamp = $_GET['timestamp'];
? ? ? ?$echostr ? = $_GET['echostr'];
? ? ? ?$signature = $_GET['signature'];
? ? ? ?//形成數(shù)組,然后按字典序排序
? ? ? ?$array = array($nonce, $timestamp, $token);
? ? ? ?sort($array);
? ? ? ?//拼接成字符串,sha1加密 ,然后與signature進行校驗
? ? ? ?$str = sha1( implode( $array ) );
? ? ? ?if( $str ?== $signature && $echostr ){
? ? ? ? ? ?//第一次接入weixin api接口的時候
? ? ? ? ? ?echo ?$echostr;
? ? ? ? ? ?exit;
? ? ? ?}else{
? ? ? ? ? ?$this->responseMsg();
? ? ? ?}
? ?}
? ?// 接收事件推送并回復(關注/取消關注)
? ?public function ?responseMsg(){
? ? ? ?//1.獲取到微信推送過來post數(shù)據(jù)(xml格式)
? ? ? ?$postArr = $GLOBALS['HTTP_RAW_POST_DATA'];
? ? ? ?//2.處理消息類型,并設置回復類型和內(nèi)容
//<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 );
? ? ? ?//判斷該數(shù)據(jù)包是否是訂閱的事件推送
? ? ? ?if( strtolower($postObj->MsgType) == 'event'){
? ? ? ? ? ?//如果是關注 subscribe 事件
? ? ? ? ? ?if( strtolower($postObj->Event) == 'subscribe'){
? ? ? ? ? ? ? ?//回復用戶消息(純文本格式)
? ? ? ? ? ? ? ?$toUser ? = $postObj->FromUserName;
? ? ? ? ? ? ? ?$fromUser = $postObj->ToUserName;
? ? ? ? ? ? ? ?$time ? ? = time();
? ? ? ? ? ? ? ?$msgType ?= 'text';
? ? ? ? ? ? ? ?$content ?= '歡迎關注我們的微信公眾賬號'.$postObj->FromoUserName.'----'.$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;
? ? ? ? ? ?}
? ? ? ?}
//<xml>
//<ToUserName><![CDATA[toUser]]></ToUserName>
//<FromUserName><![CDATA[fromUser]]></FromUserName>
//<CreateTime>1348831860</CreateTime>
//<MsgType><![CDATA[text]]></MsgType>
//<Content><![CDATA[this is a test]]></Content>
//<MsgId>1234567890123456</MsgId>
//</xml>
? ? ? ?//判斷該數(shù)據(jù)包是否是用戶發(fā)送的信息
? ? ? ?if( strtolower( $postObj->MsgType) == 'text'){
? ? ? ? ? ?$toUser ? = $postObj->FromUserName;
? ? ? ? ? ?$fromUser = $postObj->ToUserName;
? ? ? ? ? ?$time ? ? = time();
? ? ? ? ? ?$msgType ?= 'text';
? ? ? ? ? ?$content ?= 'wait';
? ? ? ? ? ?$msgId ? ?= $postObj->MsgId;
? ? ? ? ? ?$template = "<xml>
? ? ? ? ? ? ? ? ? ? ? ?<ToUserName><![CDATA[%s] ]></ToUserName>
? ? ? ? ? ? ? ? ? ? ? ?<FromUserName><![CDATA[%s] ]></FromUserName>
? ? ? ? ? ? ? ? ? ? ? ?<CreateTime>%s</CreateTime>
? ? ? ? ? ? ? ? ? ? ? ?<MsgType><![CDATA[%s] ]></MsgType>
? ? ? ? ? ? ? ? ? ? ? ?<Content><![CDATA[%s] ]></Content>
? ? ? ? ? ? ? ? ? ? ? ?<MsgId>%s</MsgId>
? ? ? ? ? ? ? ? ? ? ? ?</xml>";
? ? ? ? ? ?$info ? ? = sprintf($template, $toUser, $fromUser, $time, $msgType, $content, $msgId);
? ? ? ? ? ?echo $info;
? ? ? ?}
? ?}
}