微信公众平台 - 今天弄微信服务号的消息回复自定义php,发现用公司的服务号不能正常运行,但是用自己的公众号却可以自动回复消息

WBOY
發布: 2016-07-06 13:51:49
原創
1282 人瀏覽過

今天弄微信服务号的消息回复自定义php,发现用公司的服务号不能正常运行,但是用自己的公众号却可以自动回复消息
请问这种情况会是什么造成的

<code><?php header('Content-type:text');
define("TOKEN", "weixin");
$wechatObj = new wechatCallbackapiTest();
if (isset($_GET['echostr'])) {
    $wechatObj->valid();
}else{
    $wechatObj->responseMsg();
}

class wechatCallbackapiTest
{
    public function valid()
    {
        $echoStr = $_GET["echostr"];
        if($this->checkSignature()){
            header('content-type:text');
            echo $echoStr;
            exit;
        }
    }

    private function checkSignature()
    {
        $signature = $_GET["signature"];
        $timestamp = $_GET["timestamp"];
        $nonce = $_GET["nonce"];

        $token = TOKEN;
        $tmpArr = array($token, $timestamp, $nonce);
        sort($tmpArr, SORT_STRING);
        $tmpStr = implode( $tmpArr );
        $tmpStr = sha1( $tmpStr );

        if( $tmpStr == $signature ){
            return true;
        }else{
            return false;
        }
    }

    public function responseMsg()
    {
        $postStr = $GLOBALS["HTTP_RAW_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>";
            $msgType = "text";
            $contentStr = date("Y-m-d H:i:s",time())."\n\n".'hello';
            $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
            echo $resultStr;
        }else{
            echo "";
            exit;
        }
    }
}
?></code>
登入後複製
登入後複製

回复内容:

今天弄微信服务号的消息回复自定义php,发现用公司的服务号不能正常运行,但是用自己的公众号却可以自动回复消息
请问这种情况会是什么造成的

<code><?php header('Content-type:text');
define("TOKEN", "weixin");
$wechatObj = new wechatCallbackapiTest();
if (isset($_GET['echostr'])) {
    $wechatObj->valid();
}else{
    $wechatObj->responseMsg();
}

class wechatCallbackapiTest
{
    public function valid()
    {
        $echoStr = $_GET["echostr"];
        if($this->checkSignature()){
            header('content-type:text');
            echo $echoStr;
            exit;
        }
    }

    private function checkSignature()
    {
        $signature = $_GET["signature"];
        $timestamp = $_GET["timestamp"];
        $nonce = $_GET["nonce"];

        $token = TOKEN;
        $tmpArr = array($token, $timestamp, $nonce);
        sort($tmpArr, SORT_STRING);
        $tmpStr = implode( $tmpArr );
        $tmpStr = sha1( $tmpStr );

        if( $tmpStr == $signature ){
            return true;
        }else{
            return false;
        }
    }

    public function responseMsg()
    {
        $postStr = $GLOBALS["HTTP_RAW_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>";
            $msgType = "text";
            $contentStr = date("Y-m-d H:i:s",time())."\n\n".'hello';
            $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
            echo $resultStr;
        }else{
            echo "";
            exit;
        }
    }
}
?></code>
登入後複製
登入後複製

可以先看一下是否把自己的微信公众号配置信息当成公司的信息配置到了代码中

已解决,是两个公众号在绑定URL的时候 选择的模式不一样,选择'安全模式'会报错,选择'文明模式'能正常访问。

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!