今天突然获取openid失败,代码都没改动,1个小时之后自动恢复正常了
发布于 4 年前 作者 juanlong 7634 次浏览 来自 问答

首先声明一下,API的用法我都懂,之前一直没有问题,小程序也上线了正常使用中。

今天早上突然报告用户登录失败,检查发现是通过code获取openid的API执行失败。

代码没有做任何改动,大概1个小时之后这个接口恢复正常,能够正确返回openid了

不知道是这个接口出现的BUG,还是curl请求出现的问题。(下方贴了关键代码)


另外问一下,微信对这个接口是否有访问次数限制?


我在小程序客户端没有存储skey,用户每次打开小程序在onload里都会调用wx.login得到code,然后在服务器端使用这个code通过jscode2session这个接口获取openid,以此识别用户。


    $code = $_GET['code']; 

    $appid = '***********';

    $secret = '************************';


    function https_request($url, $post = false, $data = '')

    {

       $curl = curl_init();

       curl_setopt($curl, CURLOPT_URL, $url);

       curl_setopt($curl, CURLOPT_HEADER, 0);

       curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

       if($post)

       {

           curl_setopt($curl, CURLOPT_POST, 1);

           curl_setopt($curl, CURLOPT_POSTFIELDS, $data);

       }

       curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

       curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, true);

       $ret = curl_exec($curl);

       $curlerr = curl_error($curl);

       curl_close($curl);


       return $ret;

    }


    $url = ' https://api.weixin.qq.com/sns/jscode2session?'.'appid='.$appid.'&secret='.$secret.'&js_code='.$code;

    

    $res = json_decode( https_request ($url));

    $openid = $res->openid;

    这里的$res值为NULL,说明调用jscode2session这个api的返回值为false

2 回复

我现在突然不行,怎么办,用户等着用呀,怎么解决???

我也出现这个问题,请问楼主解决了吗?

回到顶部