zoukankan      html  css  js  c++  java
  • 微信小程序 获取用户openid

    1,可以在小程序app.js入口文件中放入登录代码

        wx.login({
          success: res => {
            // 登录注册接口
            if (res.code) {
              // 调用服务端登录接口,发送 res.code 到服务器端换取 openId, sessionKey, unionId并存入数据库中
              
            } else {
              console.log('登录失败!' + res.errMsg)
            }
          }
        });

    2,服务端PHP,小程序获取openid接口

        // 获取openid
        function getOpenid($code){ // $code为小程序提供
            $appid = ''; // 小程序APPID
            $secret = ''; // 小程序secret
            $url = 'https://api.weixin.qq.com/sns/jscode2session?appid=' . $appid . '&secret='.$secret.'&js_code='.$code.'&grant_type=authorization_code';    
                
            $curl = curl_init();
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($curl, CURLOPT_TIMEOUT, 500);
            // 为保证第三方服务器与微信服务器之间数据传输的安全性,所有微信接口采用https方式调用,必须使用下面2行代码打开ssl安全校验。    
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
            curl_setopt($curl, CURLOPT_URL, $url);
            $res = curl_exec($curl);
            curl_close($curl);
            
            return json_decode($res, true); // 这里是获取到的信息
        }    
  • 相关阅读:
    leetcode[9]Palindrome Number
    leetcode[10]Regular Expression Matching
    leetcode[11]Container With Most Water
    leetcode[12]Integer to Roman
    leetcode[13]Roman to Integer
    leetcode[14]Longest Common Prefix
    leetcode[15]3Sum
    leetcode[16]3Sum Closest
    leetcode[17]Letter Combinations of a Phone Number
    leetcode[18]4Sum
  • 原文地址:https://www.cnblogs.com/hui9527/p/9242031.html
Copyright © 2011-2022 走看看