微信小程序关于获取用户的openid的php后端代码

首先配置问题;

首先配置域名和appid(使用的是谁的appid就要在微信公众平台中配置 )

 

 

这两个配置一下就行了;

 

Tp代码:

//https://ht.likeball.top/index/index/codes
public function codes()
{
    $code = input('code');//用户的登录凭证code(使用wx.login({})可获取到)这个是前端传过来
    $appid = 'wx899****'; //微信公众平台看
    $secret = 'ddb******';//微信公众平台看
    $url = "https://api.weixin.qq.com/sns/jscode2session?appid=$appid&secret=$secret&js_code=$code&grant_type=authorization_code";
    $weixin = file_get_contents($url);
    $jsondecode = json_decode($weixin);
    $array = get_object_vars($jsondecode);
    echo json_encode($array);
}

 

小程序中js代码;

    wx.login({
      success:function(res){
        let code = res.code
        console.log(code)
        wx.request({
          url: 'https://ht.likeball.top/index/index/codes',
          data:{
             code : code
          },
          success: function (res) {
            console.log(res.data)
          }
        })
      }
    })

 

这样前端就可以获取到信息了;

清除缓存的情况下session_key是会发生变化的,但是openid是不会发生变化的。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章