小程序获取openid

2020-06-27  本文已影响0人  张浩宇_

用户授权登录时调用wx.login即可获取到code

  获取code后,请求以下链接即可获取openid: https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code

  具体详情大家看一下微信文档 :https://developers.weixin.qq.com/miniprogram/dev/api/open-api/login/wx.login.html

public function openid($code){

        $secret = "appsecret";

        $appid = "appid";

        $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appid."&secret=".$secret."&code=".$code."&grant_type=authorization_code";

        $ch = curl_init();

        curl_setopt($ch,CURLOPT_URL,$url);

        curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);

        curl_setopt($ch,CURLOPT_TIMEOUT,30);

        $content = curl_exec($ch);

        $status = (int)curl_getinfo($ch,CURLINFO_HTTP_CODE);

        if ($status == 404) {

            return $status;

        }

        curl_close($ch);

        return json_decode($content,true);

    }

上一篇下一篇

猜你喜欢

热点阅读