>  기사  >  백엔드 개발  >  微信公众号开发者自动回复设置没反应

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

WBOY
WBOY원래의
2016-06-06 20:08:112993검색

在新浪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

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.