公众号和小程序获取用户信息及获取手机号

公众号的获取用户信息
前端传code,后端根据code获取用户信息
代码:

$code = $this->request->param('code');
//通过下面url获取access_t和 openid
$url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid=' . getConfig('app_id') . '&secret=' . getConfig('app_secret') . '&code=' . $code . '&grant_type=authorization_code';//静默授权和非静默授权需要注意区分
$data = json_decode($this->curl($url));
if (empty($data)) {
    
    
    apiResponse('0', 'code失效');
}
$access_token = $data->access_token;
$openid = $data->openid;
$get_user_info_url = "https://api.weixin.qq.com/sns/userinfo?access_token=$access_token&openid=$openid&lang=zh_CN";
$userinfo = $this->getJson($get_user_info_url);
//userinfo 里面就是用户信息了
//引用的方法
function curl($url) {
    
    
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    $data = curl_exec($curl);
    curl_close($curl);
    return $data;
}
public function getJson($url) {
    
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $output = curl_exec($ch);
    curl_close($ch);
    return json_decode($output, true);
}

小程序获取用户信息
前端传iv,encryptedData,code,然后后端根据code获取session_key,再利用session_key,encryptedData及iv来获取用户信息
注意:前端wx.login获取code和获取用户信息的顺序
代码:(用的easyWeChat)

	use EasyWeChat\Factory;
	$this->wx_config = [
           'app_id' => getConfig('app_id'),
           'secret' => getConfig('app_secret'),
           'response_type' => 'array',
     ];
	$iv = $this->request->post('iv');
    $encryptedData = $this->request->post('encryptedData');
    $code = $this->request->post('code');
    if (empty($iv) || empty($encryptedData) || empty($code)) {
    
    
        apiResponse('0', "参数缺失");
    }
    $app = Factory::miniProgram($this->wx_config);
    $session_keys = $app->auth->session($code);
    if (empty($session_keys['session_key'])) {
    
    
        apiResponse('0', $session_keys['errmsg']);
    }
    $session_key = $session_keys['session_key'];
    $openid = $session_keys['openid'];


    $data = $app->encryptor->decryptData($session_key, $iv, $encryptedData);
    //data就是获取到的用户信息了

小程序获取用户手机号
前端传code,后端根据code获取用户手机号
代码:

   $access_token = $this->get_access_token();
   $url = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=" . $access_token;
   $data = '{"code":"' . $params['code'] . '"}';
   $result = curl_post($url, $data);
   $result_arr = json_decode($result, true);

   if ($result_arr['errcode'] == 0) {
    
    
       apiResponse('0', '手机号获取失败');
   }
   //保存手机号
   $result = Db::name('user')->where("uid", $user_id)->update(['mobile' => $result_arr['phone_info']['purePhoneNumber']]);
//$result_arr['phone_info']['purePhoneNumber']是我想要的手机号
  #获取access_token
  public function get_access_token() {
    
    
      $app_id = getConfig('app_id');
      $secret = getConfig('app_secret');
      $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' . $app_id . '&secret=' . $secret;
      $result = file_get_contents($url);
      $result = json_decode($result, true); //转为数组
      return $result['access_token'];
  }
  
 function curlPost($url, $data){
    
    
        $header = array(
            'Content-Type: application/json; charset=utf-8',
        );
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        $response = curl_exec($ch);
        curl_close($ch);
        return $response;
    }

猜你喜欢

转载自blog.csdn.net/zax_96/article/details/127007083