V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
vrain
V2EX  ›  问与答

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

  •  
  •   vrain · 2018-03-27 14:31:50 +08:00 · 1323 次点击
    这是一个创建于 2221 天前的主题,其中的信息可能已经有所发展或是发生改变。
    这两天正在看公众号开发的教程,为什么跟着教程走,上面的消息接口没反应呢?
    用的是微信的示例接口文件啊。


    <?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 条回复    2018-03-27 16:45:07 +08:00
    macwhirr
        1
    macwhirr  
       2018-03-27 15:31:12 +08:00
    sort($tmpArr, SORT_STRING);
    vrain
        2
    vrain  
    OP
       2018-03-27 16:33:30 +08:00
    那一行,我代码上有,复制过来的时候,没粘上。
    @macwhirr
    vrain
        3
    vrain  
    OP
       2018-03-27 16:34:55 +08:00
    @macwhirr 感谢您的回复。
    HanSonJ
        4
    HanSonJ  
       2018-03-27 16:45:07 +08:00
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   4295 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 05:32 · PVG 13:32 · LAX 22:32 · JFK 01:32
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.