php知识积累

迁移服务器导致公众号不可用

2018-12-03  本文已影响2人  hey_沙子

每个和微信相关的页面都会报如下错误:


image.png

找到错误是JSSDK文件中的获取access_token时报错

private function getAccessToken() {
    // access_token 应该全局存储与更新,以下代码以写入到文件中做示例
    $data = json_decode($this->get_php_file("access_token.php"));
    $access_token='';// 定义变量 add by ls 2018-11-20
    if ($data->expire_time < time()) {
        // 如果是企业号用以下URL获取access_token
        // $url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$this->appId&corpsecret=$this->appSecret";
        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$this->appId&secret=$this->appSecret";
        file_put_contents("/server/ls_log.txt", "--url={$url}\r\n", FILE_APPEND);

        $res = json_decode($this->httpGet($url));
        file_put_contents("/server/ls_log.txt", "--------ret=" . var_export($res, true) . "\r\n", FILE_APPEND);

        $access_token = $res->access_token;
        if ($access_token) {
            $data->expire_time = time() + 7000;
            $data->access_token = $access_token;
            $this->set_php_file("access_token.php", json_encode($data));
        }
    } else {
        $access_token = $data->access_token;
    }
    return $access_token;
}
打印日志发现如下错误码:40125
--url=https://api.weixin.qq.com/cgi-bin/token? grant_type=client_credential&appid=online12345678910&secret=online1234567890
--------ret=stdClass::__set_state(array(
   'errcode' => 40125,
   'errmsg' => 'invalid appsecret, view more at http://t.cn/RAEkdVq hint: [5iqQZa09221533]',
))

一般报错40125就是appId和appSecret不正确或者是请求获取access_token的链接中有空格

$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$this->appId&secret=$this->appSecret";

发现日志中输出的这个url没有空格,然后对比公众号后台发现appId和url中的不一样,secret现在是看不到的,但是appId不应该不正确啊,然后打开配置文件,发现日志中打印的是线上的appId,而不是beta环境,而我现在是在beta环境出现的这个bug。
最终发现配置文件没有添加软连接指向beta环境,还有就是access_token.php和jsapi_ticket.php
这两个文件的权限不够,没法写入,后来添加了权限

发现还是不可以,但是此时打印的错误信息变了: 40164

--url=https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=beta1234567890&secret=beta1234567890
--------ret=stdClass::__set_state(array(
   'errcode' => 40164,
   'errmsg' => 'invalid ip 120.133.17.222, not in whitelist hint: [M9gbfa05242994]',
))

重要的是此时的appId和secret都是beta环境的,说明刚才添加beta环境配置文件的软连接是正确的
此时40164这个的错误信息提示的是:iP没在白名单里,这个只需要在配置的白名单把错误的这个ip添加进去。
详细的添加白名单请参考
https://www.jianshu.com/p/28e6bef5f1f5
添加好了之后ok

上一篇下一篇

猜你喜欢

热点阅读