cari

Rumah  >  Soal Jawab  >  teks badan

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

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

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

    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><![CDATA[%s]]></ToUserName>
                                                    <FromUserName><![CDATA[%s]]></FromUserName>
                                                    <CreateTime>%s</CreateTime>
                                                    <MsgType><![CDATA[%s]]></MsgType>
                                                    <Content><![CDATA[%s]]></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;
            }
    }

}

?>

ringa_leeringa_lee2820 hari yang lalu715

membalas semua(2)saya akan balas

  • PHP中文网

    PHP中文网2017-04-10 17:44:37

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

    balas
    0
  • 高洛峰

    高洛峰2017-04-10 17:44:37

    这只是验证。
    看下文档里关于被动回复消息的介绍吧。
    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

    balas
    0
  • Batalbalas