微信接入的折磨

大家都知道微信要想成为微信开发者,必须在微信公众号后台配置回调url,也就是开发者服务器url ,url代表开发者接收微信事件的地址,token由开发者随意填写,之前是已经开发好的,服务器配置也弄好了,可是过了两个月发现在以同样的方式接入,总是提示:"token验证失败",真实百思不得其解;

index-wechat.jpg

分析原因

(1). 接入文件不能有BOM,(查看后没有BOM)
(2).在服务器端验证通过后,原样输出echoStr之前不能有任何输出,所有要关闭php的display_errors=off,(已确定设置)
(3).在服务器端验证通过后,写入文件,查看文件内容,(发现没有问题,输出true,说明通过服务器验证)

    private function checkSignature()
    {
        // you must define TOKEN by yourself
        if (!defined("TOKEN")) {
            throw new Exception('TOKEN is not defined!');
        }
        
        $signature = $_GET["signature"];
        $timestamp = $_GET["timestamp"];
        $nonce = $_GET["nonce"];
                
        $token = TOKEN;
        $tmpArr = array($token, $timestamp, $nonce);
        // use SORT_STRING rule
        sort($tmpArr, SORT_STRING);
        $tmpStr = implode( $tmpArr );
        $tmpStr = sha1( $tmpStr );
        
        if( $tmpStr == $signature ){
            //在这里将true写入文件
            return true;
        }else{
            return false;
        }
    }

此时微信端依然报"token验证失败"

(4) 在网上找到微信官方接入demo

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

//define your token
define("TOKEN", "weixin");
$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)){
                /* libxml_disable_entity_loader is to prevent XML eXternal Entity Injection,
                   the best way is to check the validity of xml by yourself */
                libxml_disable_entity_loader(true);
                  $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()
    {
        // you must define TOKEN by yourself
        if (!defined("TOKEN")) {
            throw new Exception('TOKEN is not defined!');
        }
        
        $signature = $_GET["signature"];
        $timestamp = $_GET["timestamp"];
        $nonce = $_GET["nonce"];
                
        $token = TOKEN;
        $tmpArr = array($token, $timestamp, $nonce);
        // use SORT_STRING rule
        sort($tmpArr, SORT_STRING);
        $tmpStr = implode( $tmpArr );
        $tmpStr = sha1( $tmpStr );
        
        if( $tmpStr == $signature ){
            return true;
        }else{
            return false;
        }
    }
}

?>

配置后请求该demo内容,微信提示"提交成功"(注意:在同一域名下)

(5) 最后分析,我们的接入文件是在thinkphp框架下的,不是直接访问的.php文件,最后解决办法是在echo $echoStr;之前ob_clean();清空(擦掉)输出缓冲区解决;

    public function valid($echoStr) 
    {
        if($this->checkSignature()) 
        {
            ob_clean();//清空(擦掉)输出缓冲区解决,这一行很关键
            echo $echoStr;//原路返回验证的随机字符串 
            exit;
        }
    }

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,237评论 19 139
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 176,823评论 25 709
  • 一、背景介绍 作为一名Android开发,从最初的跌跌撞撞到现在小有所悟,这其中经历过的辛酸苦辣也是一种痛并快乐着...
    freecats08阅读 10,457评论 7 54
  • 此方案是swift3,现在swift4出来了,估计不适用了 ,特此声明,最近有点忙(出轨去了) 没法实时更新了,抱...
    wg689阅读 12,643评论 9 29
  • “鳖可是两栖动物,怎么可能会被淹死?”“世界之大、无奇不有,朋友,小天鹅都能摔死,老鳖为什么不会被淹死?”“我还是...
    清风半月鸣蝉阅读 4,429评论 1 0

友情链接更多精彩内容