1.前端
wx.login({
complete: (res) => {
if (res.code) {
//发起网络请求
wx.request({
url: app.globalData.host_url+'/getopenid.php',
data: {
code: res.code
},complete:(res)=>{
let json = JSON.parse(res.data);
this.setData({
openid:json.openid
})
console.log(this.openid)
}
})
} else {
console.log('登录失败!' + res.errMsg)
}
},
})
2.后台
<?php
$code = $_GET["code"];
$url = "https://api.weixin.qq.com/sns/jscode2session?appid=APPID&secret=SECRET&js_code=".$code."&grant_type=authorization_code";
$res = json_encode(http_get($url),JSON_UNESCAPED_UNICODE);
file_put_contents("error.txt",$res."\r\n",FILE_APPEND);
echo $res;
/**
* 传入json数据进行HTTP Get请求
*
* @param string $url $data_string
* @return string
*/
function http_get($url)
{
$curl = curl_init(); // 启动一个CURL会话
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在
$tmpInfo = curl_exec($curl); //返回api的json对象
//关闭URL请求
curl_close($curl);
return $tmpInfo; //返回json对象
}
?>
- THE END -
最后修改:2020年6月9日
非特殊说明,本博所有文章均为博主原创。
如若转载,请注明出处:https://www.95app.top/%e5%be%ae%e4%bf%a1%e5%b0%8f%e7%a8%8b%e5%ba%8f%e8%8e%b7%e5%8f%96openid/