在小程序中获取openId,附加具体代码
1.客户端代码
// 获取 code
getLogin: function () {
var that = this;
wx.login({
success: function (res) {
that.getOpenId(res.code);
console.log(res);
}
});
},
//获取openid
getOpenId: function (code) {
var that = this;
wx.request({
url: that.url.openId,
method: 'POST',
header: {
'content-type': 'application/x-www-form-urlencoded'
},
data: { 'code': code },
success: function (res) {
var openId = res.data.openid;
console.log(res.data.openid);
// that.xiadan(openId);
}
})
},
【在小程序中获取openId,附加具体代码】2.后台 php端 代码:
//获取openId
function getOpenId($code)
{
$url = 'https://api.weixin.qq.com/sns/jscode2session?appid='
. $this->appid . '&secret='
. $this->secret . '&js_code='
. $code . '&grant_type=authorization_code';
return $this->getHTTPS($url);
}//获取https页面信息
function getHTTPS($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_REFERER, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}//封装到公共的方法之后在其它php中调用,并返回
public function openId()
{
$code = $this->request->post('code');
echo $this->getOpenId($code);
}
推荐阅读
- 一个小故事,我的思考。
- 家乡的那条小河
- 你到家了吗
- 一个人的碎碎念
- 野营记-第五章|野营记-第五章 讨伐梦魇兽
- 昨夜小楼听风
- 闲杂“细雨”
- 杜月笙的口才
- 2021-02-17|2021-02-17 小儿按摩膻中穴-舒缓咳嗽
- 赢在人生六项精进二阶Day3复盘