微信小程序關於獲取用戶的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是不會發生變化的。

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