亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

求助,關注后沒有消息推送?發文本后發出“該公眾號提供的服務出現故障,請稍后再試”

代碼如下,應該跟視頻差不多,不清楚,看了挺多解釋也不是很明白

<?php
namespace Home\Controller;
use Think\Controller;
class IndexController 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($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數據(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 );
? ? ? ?//判斷該數據包是否是訂閱的事件推送
? ? ? ?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>
? ? ? ?//判斷該數據包是否是用戶發送的信息
? ? ? ?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;
? ? ? ?}
? ?}
}



正在回答

舉報

0/150
提交
取消

求助,關注后沒有消息推送?發文本后發出“該公眾號提供的服務出現故障,請稍后再試”

我要回答 關注問題
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號