【黄啊码】微信小程序登录怎么做?如何获取open_id

举报
黄啊码 发表于 2022/06/29 00:56:19 2022/06/29
【摘要】 getOpenid:function(){ var that = this; return new Promise(function (resolve, reject){ wx.login({ success: function (res) { if (re...

   
  1. getOpenid:function(){
  2. var that = this;
  3. return new Promise(function (resolve, reject){
  4. wx.login({
  5. success: function (res) {
  6. if (res.code) {
  7. //发起网络请求
  8. wx.request({
  9. url: config.api+'/api/v1.user/getUserInfo',
  10. data: {
  11. code:res.code //微信小程序生成的code
  12. },
  13. success: function (res) {
  14. console.log("登录成功");
  15. }
  16. });
  17. }
  18. }
  19. });
  20. }).then(function(){
  21. });
  22. },

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'];


  
  1. function curlGet($url)
  2. {
  3. $ch = curl_init();
  4. $header = [
  5. "Content-Type: application/json;charset=UTF-8",
  6. ];
  7. curl_setopt($ch, CURLOPT_URL, $url);
  8. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
  9. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  10. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  11. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  12. curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
  13. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  14. curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
  15. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  16. $temp = curl_exec($ch);
  17. return $temp;
  18. }

文章来源: markwcm.blog.csdn.net,作者:黄啊码,版权归原作者所有,如需转载,请联系作者。

原文链接:markwcm.blog.csdn.net/article/details/120908152

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。