微信小程序 获取微信用户的 openid

// 客户端 微信小程序 获取微信用户的 openid 处理
onLaunch: function () {
// 登录
wx.login({
success: res => {

console.log( "res.code:" + res.code);

// 发送 res.code 到后台换取 openId, sessionKey, unionId
wx.request({
     url: 'https://你的服务器网址/WxgetOpenid.php', //获取到的openid session_key
data: {
code: res.code
},
method: 'GET',
     success: function (resuser) {

    console.log( "open_id:" + resuser.data.openid);
    }
})
}
})
}

===============================================================

//   服务器端  WxgetOpenid.php 程序

<?php

   //   获取  微信小程序应用用户的 openid 
    $code = $_GET[code];    //传进的code 参数

    $App_ID       =  "wxea===========";                            //在小程序平台注册应用的应用ID号
    $App_Secret =  "========================" ;   //在小程序平台注册应用的应用secret号

    $url = "https://api.weixin.qq.com/sns/jscode2sessionappid=$App_ID&secret=$App_Secret&
                js_code=$code&grant_type=authorization_code";


      $apiData = file_get_contents($url);         // 返回的是json_encode  无需转换处理

echo $apiData ;
?>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值