課程
/后端開發
/PHP
/ PHP實現微信公眾平臺開發—提升篇
關注不回復 不進else?
2017-12-24
源自: PHP實現微信公眾平臺開發—提升篇 3-1
正在回答
請問,你解決了嗎
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];很可能是這行代碼有問題,這行代碼我是獲取不到數據的,所以進不了判斷。$postStr =file_get_contents("php://input"); 改用這句就可以獲取xml數據,然后進入判斷了。
這個是代碼? token已經配置成功了 大神看看為啥關注成功不回復消息?
<?php
namespace app\index\controller;
class Index
{
public function index(){
//獲得參數 signature nonce token timestamp echostr
$nonce? ? ?= $_GET['nonce'];
$token? ? ?= 'imooc';
$timestamp = $_GET['timestamp'];
$echostr? ?= $_GET['echostr'];
$signature = $_GET['signature'];
//形成數組,然后按字典序排序
$array = array();
$array = array($nonce, $timestamp, $token);
sort($array);
//拼接成字符串,sha1加密 ,然后與signature進行校驗
$str = sha1( implode( $array ) );
if( $str == $signature && $echostr){
//第一次接入weixin api接口的時候
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') {
//判斷如果是關注
? ? ? ? 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 = "謝謝關注000";
? ? ? ? ? ? ? ? $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
? ? ? ? ? ? ? ? echo $resultStr;
? ? ? ? ? ? }
? ? ? ? }
}//reponseMsg end
我的也不行,怎么解決?。啃值?/p>
不進else多半是代碼有錯,要貼代碼才好找
舉報
本課程詳細講解了接收微信用戶發送的消息和接收訂閱關注的事件推送
1 回答關注后不回復
3 回答驗證成功,但關注后無法收到回復消息!
1 回答判斷是否回復成功
1 回答關注沒有回復
6 回答消息回復的時候不知道錯到哪里了 就是不成功
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網安備11010802030151號
購課補貼聯系客服咨詢優惠詳情
慕課網APP您的移動學習伙伴
掃描二維碼關注慕課網微信公眾號
2018-09-20
請問,你解決了嗎
2018-02-17
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];很可能是這行代碼有問題,這行代碼我是獲取不到數據的,所以進不了判斷。$postStr =file_get_contents("php://input"); 改用這句就可以獲取xml數據,然后進入判斷了。
2017-12-27
這個是代碼? token已經配置成功了 大神看看為啥關注成功不回復消息?
2017-12-27
<?php
namespace app\index\controller;
class Index
{
public function index(){
//獲得參數 signature nonce token timestamp echostr
$nonce? ? ?= $_GET['nonce'];
$token? ? ?= 'imooc';
$timestamp = $_GET['timestamp'];
$echostr? ?= $_GET['echostr'];
$signature = $_GET['signature'];
//形成數組,然后按字典序排序
$array = array();
$array = array($nonce, $timestamp, $token);
sort($array);
//拼接成字符串,sha1加密 ,然后與signature進行校驗
$str = sha1( implode( $array ) );
if( $str == $signature && $echostr){
//第一次接入weixin api接口的時候
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') {
//判斷如果是關注
? ? ? ? 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 = "謝謝關注000";
? ? ? ? ? ? ? ? $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
? ? ? ? ? ? ? ? echo $resultStr;
? ? ? ? ? ? }
? ? ? ? }
}//reponseMsg end
}
2017-12-25
我的也不行,怎么解決?。啃值?/p>
2017-12-25
不進else多半是代碼有錯,要貼代碼才好找