首頁 > 後端開發 > php教程 > 微信公众号开发者自动回复设置没反应

微信公众号开发者自动回复设置没反应

WBOY
發布: 2016-06-06 20:08:11
原創
3070 人瀏覽過

在新浪sea平台设置了微信公众号应用,设置关注自动回复功能,接口设置成功,,代码写好,url也修改代码的页面,但是测试后没反应。

//define your token
define("TOKEN", "weixin");
$wechatObj = new wechatCallbackapiTest();
$wechatObj->valid();
$wechatObj->responseMsg();
class wechatCallbackapiTest
{

<code>    public function valid()
{
    $echoStr = $_GET["echostr"];

    //valid signature , option
    if($this->checkSignature()){
            echo $echoStr;
            exit;
    }
}

public function responseMsg()
{
            //get post data, May be due to the different environments
            $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];

    //extract post data
            if (!empty($postStr)){
            
            $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
            $fromUsername = $postObj->FromUserName;
            $toUsername = $postObj->ToUserName;
            $keyword = trim($postObj->Content); 
            $time = time();
            $textTpl = "<xml>
                                                    <tousername></tousername>
                                                    <fromusername></fromusername>
                                                    <createtime>%s</createtime>
                                                    <msgtype></msgtype>
                                                    <content></content>
                                                    <funcflag>0</funcflag>
                                                    </xml>";             
                            if(!empty( $keyword ))
            {
                    $msgType = "text";
                    $contentStr = "Welcome to wechat world!";
                    $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
                    echo $resultStr;
            }else{
                    echo "Input something...";
            }

    }else {
            echo "";
            exit;
    }
}
            
    private function checkSignature()
    {
    $signature = $_GET["signature"];
    $timestamp = $_GET["timestamp"];
    $nonce = $_GET["nonce"];        
                    
            $token = TOKEN;
            $tmpArr = array($token, $timestamp, $nonce);
            sort($tmpArr);
            $tmpStr = implode( $tmpArr );
            $tmpStr = sha1( $tmpStr );
            
            if( $tmpStr == $signature ){
                    return true;
            }else{
                    return false;
            }
    }</code>
登入後複製
登入後複製

}

?>

回复内容:

在新浪sea平台设置了微信公众号应用,设置关注自动回复功能,接口设置成功,,代码写好,url也修改代码的页面,但是测试后没反应。

//define your token
define("TOKEN", "weixin");
$wechatObj = new wechatCallbackapiTest();
$wechatObj->valid();
$wechatObj->responseMsg();
class wechatCallbackapiTest
{

<code>    public function valid()
{
    $echoStr = $_GET["echostr"];

    //valid signature , option
    if($this->checkSignature()){
            echo $echoStr;
            exit;
    }
}

public function responseMsg()
{
            //get post data, May be due to the different environments
            $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];

    //extract post data
            if (!empty($postStr)){
            
            $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
            $fromUsername = $postObj->FromUserName;
            $toUsername = $postObj->ToUserName;
            $keyword = trim($postObj->Content); 
            $time = time();
            $textTpl = "<xml>
                                                    <tousername></tousername>
                                                    <fromusername></fromusername>
                                                    <createtime>%s</createtime>
                                                    <msgtype></msgtype>
                                                    <content></content>
                                                    <funcflag>0</funcflag>
                                                    </xml>";             
                            if(!empty( $keyword ))
            {
                    $msgType = "text";
                    $contentStr = "Welcome to wechat world!";
                    $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
                    echo $resultStr;
            }else{
                    echo "Input something...";
            }

    }else {
            echo "";
            exit;
    }
}
            
    private function checkSignature()
    {
    $signature = $_GET["signature"];
    $timestamp = $_GET["timestamp"];
    $nonce = $_GET["nonce"];        
                    
            $token = TOKEN;
            $tmpArr = array($token, $timestamp, $nonce);
            sort($tmpArr);
            $tmpStr = implode( $tmpArr );
            $tmpStr = sha1( $tmpStr );
            
            if( $tmpStr == $signature ){
                    return true;
            }else{
                    return false;
            }
    }</code>
登入後複製
登入後複製

}

?>

valid方式是用来检测所发消息是否来自微信服务器的,它会输出内容到页面中来,所以会打乱你要回复的XML包
只调用responseMsg方法即可

这只是验证。
看下文档里关于被动回复消息的介绍吧。
http://mp.weixin.qq.com/wiki/1/6239b44c206cab9145b1d52c67e6c551.html#.E5.9B.9E.E5.A4.8D.E6.96.87.E6.9C.AC.E6.B6.88.E6.81.AF

相關標籤:
php
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板