PHP請(qǐng)求微信接口獲取用戶電話號(hào)功能示例
業(yè)務(wù)場(chǎng)景是為了在用戶登錄的時(shí)候判斷其是否已經(jīng)成功注冊(cè),沒(méi)有成功注冊(cè)的話就將獲取到的openid和session_key加密后作為token傳給前端,然后讓前臺(tái)通過(guò)組件獲得code之后連著token一起傳給后端,后端拿著code再去請(qǐng)求微信接口獲取到用戶的電話號(hào)碼,以此完成注冊(cè)。
實(shí)現(xiàn)過(guò)程中的問(wèn)題結(jié)合微信官方手冊(cè):phonenumber.getPhoneNumber | 微信開(kāi)放文檔 (qq.com)
怪我沒(méi)好好看手冊(cè),中間發(fā)生了hin多的插曲。比如報(bào)錯(cuò)返回:
require POST method hint errcode: 43002
一查文檔告訴我:這個(gè)請(qǐng)求需要用post請(qǐng)求!可是,我明明是用的post請(qǐng)求啊~~~
后面通過(guò)面向百度編程,在找了5678個(gè)公共發(fā)起post請(qǐng)求的方法之后,終于有一個(gè)post請(qǐng)求沒(méi)問(wèn)題,但又遇到了一個(gè)問(wèn)題,他返回: [0,null]
這里的原因是比較讓我耗費(fèi)時(shí)間的:這個(gè)接口的請(qǐng)求,必須在用戶處于登錄的條件下,并且必須在互聯(lián)網(wǎng)能夠訪問(wèn)到的公共網(wǎng)站上(也就是得在我的項(xiàng)目配置好的域名下去請(qǐng)求,才能夠返回值!)我在本地試了好久,氣煞我也!
如果你后面寫(xiě)好了對(duì)返回值的判斷的話會(huì)報(bào)錯(cuò):
Trying to access array offset on value of type null 。
就是告訴你不能嘗試將 null,bool,int,float 或 resource 類型的值用作數(shù)組 ( 例如 $null[“key”] ) 會(huì)產(chǎn)生一個(gè)通知。
遇到的這個(gè)問(wèn)題我是萬(wàn)萬(wàn)妹想到,搞了整整一下午,最后在公司大佬的幫助下半個(gè)小時(shí)幫我解決了問(wèn)題。ps:第一個(gè)參數(shù)access_token那是輕輕松松(有問(wèn)題可以看看和我的代碼哪里不同)
廢話不多say,上代碼!common.php中
/** * 發(fā)送curl get * @param string $url * @return mixed */function curl_get($url){ $oCurl = curl_init(); if (stripos($url, 'https://') !== FALSE) {curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, FALSE);curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1 } if (defined('CURLOPT_IPRESOLVE') && defined('CURL_IPRESOLVE_V4')) {curl_setopt($oCurl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4); } curl_setopt($oCurl, CURLOPT_URL, $url); curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1); $sContent = curl_exec($oCurl); $aStatus = curl_getinfo($oCurl); curl_close($oCurl); if (intval($aStatus['http_code']) == 200) {return $sContent; } else {return false; }}if (!function_exists('http_post_json')){ //這一行是判斷公共方法有無(wú)這個(gè)方法,避免重名~ /** * PHP發(fā)送Json對(duì)象數(shù)據(jù) * @param $url string * @param $jsonStr string * @param string[] $headers * @return array */ function http_post_json(string $url, string $jsonStr, array $headers = array('Content-Type: application/json; charset=utf-8', )): array {$headers[] = 'Content-Length: ' . strlen($jsonStr);$ch = curl_init();curl_setopt($ch, CURLOPT_POST, 1);curl_setopt($ch, CURLOPT_URL, $url);curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonStr);curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);$response = curl_exec($ch);$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);curl_close($ch);return array($httpCode, $response); }}調(diào)用接口代碼:(有空可以自己封裝一下~)
/**必須先進(jìn)入登錄狀態(tài),然后拿到phone的code去請(qǐng)求然后拿到access_code,請(qǐng)求phone的接口 */ $appid = getConfig('appid_y'); //填寫(xiě)自己的appid,小程序中看 $secret = getConfig('secret_y'); //填自己的secret,公眾平臺(tái)看 $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$secret'; $access_token = json_decode(curl_get($url),true);if(isset($access_token['errcode']))return ['errcode'=>$access_token['errcode'],'msg'=>'請(qǐng)求失敗','data'=>$access_token]; $access_token = $access_token['access_token']; //獲取到了access_token //請(qǐng)求電話號(hào)使用方法只能在公網(wǎng)能訪問(wèn)的目錄下進(jìn)行,本地進(jìn)行沒(méi)有返回值 $url = 'https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token='.$access_token; $json_code = json_encode(['code'=>$param['code']]); $headers = ['Accept: application/json','User-Agent: */*','Content-Type: application/json; charset=utf-8', ]; $phone = http_post_json($url,$json_code,$headers); $phone[1] = json_decode($phone[1],true); if(empty($phone[1])||$phone[1]['errcode']!=0)throw new Exception('系統(tǒng)獲取手機(jī)號(hào)失敗'); $phoneNumber = $phone[1]['phone_info']['phoneNumber']; /**拿到電話號(hào)碼end */另外,thinkphp5獲取微信授權(quán)用戶手機(jī)號(hào)的相關(guān)實(shí)現(xiàn)方法,可參考前面一篇:https://www.jb51.net/article/229956.htm
相關(guān)文章:
1. windows服務(wù)器使用IIS時(shí)thinkphp搜索中文無(wú)效問(wèn)題2. Nginx+php配置文件及原理解析3. 用PHP+MySql編寫(xiě)聊天室4. php的mysql性能優(yōu)化5. PHP5.0正式發(fā)布 不完全兼容PHP4 新增多項(xiàng)功能6. PHP laravel實(shí)現(xiàn)基本路由配置詳解7. PHP輸出控制功能在簡(jiǎn)繁體轉(zhuǎn)換中的應(yīng)用 8. Thinkphp3.2.3反序列化漏洞實(shí)例分析9. 微信小程序視圖層莫名出現(xiàn)豎線的解決方法10. Thinkphp5文件包含漏洞解析
