【黄啊码】微信小程序登录怎么做?如何获取open_id
【摘要】
getOpenid:function(){ var that = this; return new Promise(function (resolve, reject){ wx.login({ success: function (res) { if (re...
getOpenid:function(){ var that = this; return new Promise(function (resolve, reject){ wx.login({ success: function (res) { if (res.code) { //发起网络请求 wx.request({ url: config.api+'/api/v1.user/getUserInfo', data: { code:res.code //微信小程序生成的code }, success: function (res) { console.log("登录成功"); } }); } } }); }).then(function(){ }); },PHP代码:
$url_get = 'https://api.weixin.qq.com/sns/jscode2session?grant_type=authorization_code&appid=' . config("appid") . '&secret=' . config("appsecret") . '&js_code=' . $code;
$result = json_decode(curlGet($url_get),true);
$open_id = $result['openid'];
-
function curlGet($url)
-
{
-
$ch = curl_init();
-
$header = [
-
"Content-Type: application/json;charset=UTF-8",
-
];
-
curl_setopt($ch, CURLOPT_URL, $url);
-
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
-
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
-
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
-
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
-
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
-
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
-
curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
-
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
-
$temp = curl_exec($ch);
-
return $temp;
-
}
文章来源: markwcm.blog.csdn.net,作者:黄啊码,版权归原作者所有,如需转载,请联系作者。
原文链接:markwcm.blog.csdn.net/article/details/120908152
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)