vrain
V2EX  ›  问与答

微信公众号开发接口变化了吗?

  •  
  •   vrain · Mar 27, 2018 · 2015 views
    This topic created in 2977 days ago, the information mentioned may be changed or developed.
    这两天正在看公众号开发的教程,为什么跟着教程走,上面的消息接口没反应呢?
    用的是微信的示例接口文件啊。


    <?php
    /**
    * wechat php test
    */

    //define your token
    define("TOKEN", "weixin");//这里的 TOKEN,我已经在公众号后台设了
    $wechatObj = new wechatCallbackapiTest();
    $wechatObj->valid();

    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;
    }
    }
    }

    ?>
    4 replies    2018-03-27 16:45:07 +08:00
    toomwan
        1
    toomwan  
       Mar 27, 2018
    sort($tmpArr, SORT_STRING);
    vrain
        2
    vrain  
    OP
       Mar 27, 2018
    那一行,我代码上有,复制过来的时候,没粘上。
    @macwhirr
    vrain
        3
    vrain  
    OP
       Mar 27, 2018
    @macwhirr 感谢您的回复。
    HanSonJ
        4
    HanSonJ  
       Mar 27, 2018
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1147 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 35ms · UTC 23:05 · PVG 07:05 · LAX 16:05 · JFK 19:05
    ♥ Do have faith in what you're doing.