為什么關(guān)注不能自動(dòng)回復(fù)
為什么關(guān)注沒有自動(dòng)回復(fù),求大神們指教,代碼是找著老師打的。
<?php
header("Content-type: text/html; charset=utf-8");?
// 本類由系統(tǒng)自動(dòng)生成,僅供測(cè)試用途
class IndexAction extends Action?
{
? ? public function index()
? ? {
//$this->show('<style type="text/css">*{ padding: 0; margin: 0; } div{ padding: 4px 48px;} body{ background: #fff; font-family: "微軟雅黑"; color: #333;} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.8em; font-size: 36px }</style><div style="padding: 24px 48px;"> <h1>:)</h1><p>歡迎使用 <b>ThinkPHP</b>!</p></div><script type="text/javascript" src="http://tajs.qq.com/stats?sId=9347272" charset="UTF-8"></script>','utf-8');
//獲得參數(shù) signature nonce token timestamp echostr
$nonce ? ? = $_GET['nonce'];
$token ? ? = "weixin";
$timestamp = $_GET['timestamp'];
$echostr ? = $_GET['echostr'];
$signature = $_GET['signature'];
//形成數(shù)組,然后按字典序排序
$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->responseMsg();
}
public function responseMsg()
{
//1.獲取到微信推送過來post數(shù)據(jù)(xml格式)
$postArr = $GLOBALS['HTTP_RAW_POST_DATA'];
//2.處理消息類型,并設(shè)置回復(fù)類型和內(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 );
//$postObj->ToUserName = '';
//$postObj->FromUserName = '';
//$postObj->CreateTime = '';
//$postObj->MsgType = '';
//$postObj->Event = '';
// gh_e79a177814ed
//判斷該數(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)注我們的微信公眾賬號(hào)'.$postObj->FromUserName.'-'.$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;
}
? ? }
}
}
}
2018-03-01
$postArr = isset($GLOBALS['HTTP_RAW_POST_DATA']) ? $GLOBALS['HTTP_RAW_POST_DATA'] : file_get_contents("php://input"); ?//最佳解決方式,適用tp所有版本
2017-04-20
你要把你的代碼貼出來,才能看哪里出問題了!