PHP請求微信接口獲取用戶電話號功能示例
業務場景是為了在用戶登錄的時候判斷其是否已經成功注冊,沒有成功注冊的話就將獲取到的openid和session_key加密后作為token傳給前端,然后讓前臺通過組件獲得code之后連著token一起傳給后端,后端拿著code再去請求微信接口獲取到用戶的電話號碼,以此完成注冊。
實現過程中的問題結合微信官方手冊:phonenumber.getPhoneNumber | 微信開放文檔 (qq.com)
怪我沒好好看手冊,中間發生了hin多的插曲。比如報錯返回:
require POST method hint errcode: 43002
一查文檔告訴我:這個請求需要用post請求!可是,我明明是用的post請求啊~~~
后面通過面向百度編程,在找了5678個公共發起post請求的方法之后,終于有一個post請求沒問題,但又遇到了一個問題,他返回: [0,null]
這里的原因是比較讓我耗費時間的:這個接口的請求,必須在用戶處于登錄的條件下,并且必須在互聯網能夠訪問到的公共網站上(也就是得在我的項目配置好的域名下去請求,才能夠返回值!)我在本地試了好久,氣煞我也!
如果你后面寫好了對返回值的判斷的話會報錯:
Trying to access array offset on value of type null 。
就是告訴你不能嘗試將 null,bool,int,float 或 resource 類型的值用作數組 ( 例如 $null[“key”] ) 會產生一個通知。
遇到的這個問題我是萬萬妹想到,搞了整整一下午,最后在公司大佬的幫助下半個小時幫我解決了問題。ps:第一個參數access_token那是輕輕松松(有問題可以看看和我的代碼哪里不同)
廢話不多say,上代碼!common.php中
/** * 發送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')){ //這一行是判斷公共方法有無這個方法,避免重名~ /** * PHP發送Json對象數據 * @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); }}調用接口代碼:(有空可以自己封裝一下~)
/**必須先進入登錄狀態,然后拿到phone的code去請求然后拿到access_code,請求phone的接口 */ $appid = getConfig('appid_y'); //填寫自己的appid,小程序中看 $secret = getConfig('secret_y'); //填自己的secret,公眾平臺看 $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'=>'請求失敗','data'=>$access_token]; $access_token = $access_token['access_token']; //獲取到了access_token //請求電話號使用方法只能在公網能訪問的目錄下進行,本地進行沒有返回值 $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('系統獲取手機號失敗'); $phoneNumber = $phone[1]['phone_info']['phoneNumber']; /**拿到電話號碼end */另外,thinkphp5獲取微信授權用戶手機號的相關實現方法,可參考前面一篇:https://www.jb51.net/article/229956.htm
相關文章:
