// 客户端 微信小程序 获取微信用户的 openid 处理
onLaunch:
function () {
// 登录
wx.login({
success: res => {
console.log(
"res.code:" + res.code);
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 ;
?>