公眾號的獲取用戶信息
前端傳code,后端根據(jù)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';//靜默授權(quán)和非靜默授權(quán)需要注意區(qū)分
$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,然后后端根據(jù)code獲取session_key,再利用session_key,encryptedData及iv來獲取用戶信息
注意:前端wx.login獲取code和獲取用戶信息的順序
代碼:(用的easyWeChat)文章來源:http://www.zghlxwxcb.cn/news/detail-504780.html
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', "參數(shù)缺失");
}
$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就是獲取到的用戶信息了
小程序獲取用戶手機(jī)號
前端傳code,后端根據(jù)code獲取用戶手機(jī)號
代碼:文章來源地址http://www.zghlxwxcb.cn/news/detail-504780.html
$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', '手機(jī)號獲取失敗');
}
//保存手機(jī)號
$result = Db::name('user')->where("uid", $user_id)->update(['mobile' => $result_arr['phone_info']['purePhoneNumber']]);
//$result_arr['phone_info']['purePhoneNumber']是我想要的手機(jī)號
#獲取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); //轉(zhuǎn)為數(shù)組
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;
}
到了這里,關(guān)于公眾號和小程序獲取用戶信息及獲取手機(jī)號的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!