照搬下來的 也沒有回復(fù)消息 TOKEN驗(yàn)證已經(jīng)成功
<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller{
public function __construct(){
}
? ? 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.將排序后的三個(gè)參數(shù)拼接之后sha1加密
$tmpstr ?= sha1( implode($array) );
//1.將加密后的字符串與signature對比,判斷該請求是否來自微信
if( $tmpstr == $signature && $echostr){
//第一次接入微信API的時(shí)候進(jìn)行驗(yàn)證
echo $echostr;
exit;
}else{
$this->responseMsg();
}
? ? }
//接受事件推送并回復(fù)
public function responseMsg(){
//1.獲取到微信推送過來的poat數(shù)據(jù)(xml格式)
$postArr ?= $GLOBALS['HTTP_RAW_POST_DATA'];
//2.處理消息類型,并設(shè)置回復(fù)類型和內(nèi)容
$postObj ?= simplexml_load_string($postArr);
//判斷推動(dòng)的數(shù)據(jù)包是不是訂閱事件
if(strtolower($postObj->MsgType) == 'event'){
//如果是關(guān)注事件 subscribe 事件
if(strtolower($postObj->Event) == 'subscribe'){
//回復(fù)用戶消息
$toUser = $postObj->FromUserName;
$fromUser = $postObj->ToUserName;
$time = time();
$msgType ? ?= 'text';
$content ? ?= '歡迎關(guān)注我們';
$tamplate = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Content><![CDATA[%s]]></Content>
</xml>";
$info = sprintf($tamplate,$fromUser,$toUser,$time,$msgType,$content);
echo $info;
}
}
if(strtolower($postObj->MsgType) == 'text'){
if($postObj->Content == 'you'){
$tamplate = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Content><![CDATA[%s]]></Content>
</xml>";
$fromUser = $postObj->ToUserName;
$toUser = $postObj->FromUserName;
$time = time();
$content ? ?= '這個(gè)網(wǎng)站很不錯(cuò)';
$msgType ? ?= 'text';
echo ?sprintf($tamplate,$toUser,$fromUser,$time,$msgType,$content);
}
}
}
}
2018-01-04
一樣的,代碼百分之百沒錯(cuò),但就是不回復(fù)
2017-07-06
求解,token已經(jīng)驗(yàn)證成功,關(guān)注后還是沒有回復(fù)成功,代碼都確保一致了
2017-05-07
//$postArr = $GLOBALS['HTTP_RAW_POST_DATA']; ?這個(gè)有點(diǎn)問題,獲取不到數(shù)據(jù) 用下面這個(gè)吧?
$postArr?= file_get_contents('php://input');
2017-04-28
我用TP5按代碼寫還是獲取不到回復(fù)消息
2017-04-28
關(guān)注之后不回復(fù),可能是你代碼中的$info=sprintf()中的參數(shù)跟$tamplate中的<xml>中的代碼沒有對應(yīng)上,要先寫$toUser,再寫$fromUser
2017-04-28
服務(wù)器地址這塊要加上,,才能提交成功