課程
/后端開(kāi)發(fā)
/PHP
/ PHP實(shí)現(xiàn)微信公眾平臺(tái)開(kāi)發(fā)—提升篇
關(guān)注不回復(fù) 不進(jìn)else?
2017-12-24
源自: PHP實(shí)現(xiàn)微信公眾平臺(tái)開(kāi)發(fā)—提升篇 3-1
正在回答
請(qǐng)問(wèn),你解決了嗎
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];很可能是這行代碼有問(wèn)題,這行代碼我是獲取不到數(shù)據(jù)的,所以進(jìn)不了判斷。$postStr =file_get_contents("php://input"); 改用這句就可以獲取xml數(shù)據(jù),然后進(jìn)入判斷了。
這個(gè)是代碼? token已經(jīng)配置成功了 大神看看為啥關(guān)注成功不回復(fù)消息?
<?php
namespace app\index\controller;
class Index
{
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();
$array = array($nonce, $timestamp, $token);
sort($array);
//拼接成字符串,sha1加密 ,然后與signature進(jìn)行校驗(yàn)
$str = sha1( implode( $array ) );
if( $str == $signature && $echostr){
//第一次接入weixin api接口的時(shí)候
echo? $echostr;
exit;
}else{
$this->reponseMsg();
}
public function reponseMsg(){
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
? ? ? ? //??xmlêy?Y°ü×a3é???ó
? ? ? ? $postObj = simplexml_load_string($postStr);
? ? ? ? $fromUsername = $postObj->FromUserName;
? ? ? ? $toUsername = $postObj->ToUserName;
? ? ? ? $time = time();
? ? ? ? if(strtolower($postObj->MsgType) == 'event') {
//判斷如果是關(guān)注
? ? ? ? if(strtolower($postObj->Event) == 'subscribe'){
? ? ? ? ? ? ? ? $textTpl = "<xml>
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? <ToUserName><![CDATA[%s]]></ToUserName>
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? <FromUserName><![CDATA[%s]]></FromUserName>
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? <CreateTime>%s</CreateTime>
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? <MsgType><![CDATA[%s]]></MsgType>
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? <Content><![CDATA[%s]]></Content>
? ? ? ? ? ? ? ? ? ? ? ? ? ? </xml>";
? ? ? ? ? ? ? ? $msgType = "text";
? ? ? ? ? ? ? ? $contentStr = "謝謝關(guān)注000";
? ? ? ? ? ? ? ? $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
? ? ? ? ? ? ? ? echo $resultStr;
? ? ? ? ? ? }
? ? ? ? }
}//reponseMsg end
我的也不行,怎么解決?。啃值?/p>
不進(jìn)else多半是代碼有錯(cuò),要貼代碼才好找
舉報(bào)
本課程詳細(xì)講解了接收微信用戶發(fā)送的消息和接收訂閱關(guān)注的事件推送
1 回答關(guān)注后不回復(fù)
3 回答驗(yàn)證成功,但關(guān)注后無(wú)法收到回復(fù)消息!
1 回答判斷是否回復(fù)成功
1 回答關(guān)注沒(méi)有回復(fù)
6 回答消息回復(fù)的時(shí)候不知道錯(cuò)到哪里了 就是不成功
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號(hào)-11 京公網(wǎng)安備11010802030151號(hào)
購(gòu)課補(bǔ)貼聯(lián)系客服咨詢優(yōu)惠詳情
慕課網(wǎng)APP您的移動(dòng)學(xué)習(xí)伙伴
掃描二維碼關(guān)注慕課網(wǎng)微信公眾號(hào)
2018-09-20
請(qǐng)問(wèn),你解決了嗎
2018-02-17
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];很可能是這行代碼有問(wèn)題,這行代碼我是獲取不到數(shù)據(jù)的,所以進(jìn)不了判斷。$postStr =file_get_contents("php://input"); 改用這句就可以獲取xml數(shù)據(jù),然后進(jìn)入判斷了。
2017-12-27
這個(gè)是代碼? token已經(jīng)配置成功了 大神看看為啥關(guān)注成功不回復(fù)消息?
2017-12-27
<?php
namespace app\index\controller;
class Index
{
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();
$array = array($nonce, $timestamp, $token);
sort($array);
//拼接成字符串,sha1加密 ,然后與signature進(jìn)行校驗(yàn)
$str = sha1( implode( $array ) );
if( $str == $signature && $echostr){
//第一次接入weixin api接口的時(shí)候
echo? $echostr;
exit;
}else{
$this->reponseMsg();
}
}
public function reponseMsg(){
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
? ? ? ? //??xmlêy?Y°ü×a3é???ó
? ? ? ? $postObj = simplexml_load_string($postStr);
? ? ? ? $fromUsername = $postObj->FromUserName;
? ? ? ? $toUsername = $postObj->ToUserName;
? ? ? ? $time = time();
? ? ? ? if(strtolower($postObj->MsgType) == 'event') {
//判斷如果是關(guān)注
? ? ? ? if(strtolower($postObj->Event) == 'subscribe'){
? ? ? ? ? ? ? ? $textTpl = "<xml>
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? <ToUserName><![CDATA[%s]]></ToUserName>
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? <FromUserName><![CDATA[%s]]></FromUserName>
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? <CreateTime>%s</CreateTime>
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? <MsgType><![CDATA[%s]]></MsgType>
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? <Content><![CDATA[%s]]></Content>
? ? ? ? ? ? ? ? ? ? ? ? ? ? </xml>";
? ? ? ? ? ? ? ? $msgType = "text";
? ? ? ? ? ? ? ? $contentStr = "謝謝關(guān)注000";
? ? ? ? ? ? ? ? $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
? ? ? ? ? ? ? ? echo $resultStr;
? ? ? ? ? ? }
? ? ? ? }
}//reponseMsg end
}
2017-12-25
我的也不行,怎么解決?。啃值?/p>
2017-12-25
不進(jìn)else多半是代碼有錯(cuò),要貼代碼才好找