公众号和小程序获取用户信息及获取手机号
短信预约 -IT技能 免费直播动态提醒
公众号的获取用户信息
前端传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; }
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341