国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術文章
文章詳情頁

PHP請求微信接口獲取用戶電話號功能示例

瀏覽:122日期:2022-06-13 16:56:16
前言情景劇

業(yè)務場景是為了在用戶登錄的時候判斷其是否已經(jīng)成功注冊,沒有成功注冊的話就將獲取到的openid和session_key加密后作為token傳給前端,然后讓前臺通過組件獲得code之后連著token一起傳給后端,后端拿著code再去請求微信接口獲取到用戶的電話號碼,以此完成注冊。

實現(xiàn)過程中的問題

結合微信官方手冊:phonenumber.getPhoneNumber | 微信開放文檔 (qq.com)

怪我沒好好看手冊,中間發(fā)生了hin多的插曲。比如報錯返回:

require POST method hint errcode: 43002

一查文檔告訴我:這個請求需要用post請求!可是,我明明是用的post請求啊~~~

后面通過面向百度編程,在找了5678個公共發(fā)起post請求的方法之后,終于有一個post請求沒問題,但又遇到了一個問題,他返回: [0,null]

這里的原因是比較讓我耗費時間的:這個接口的請求,必須在用戶處于登錄的條件下,并且必須在互聯(lián)網(wǎng)能夠訪問到的公共網(wǎng)站上(也就是得在我的項目配置好的域名下去請求,才能夠返回值?。┪以诒镜卦嚵撕镁茫瑲馍肺乙?!

如果你后面寫好了對返回值的判斷的話會報錯:

Trying to access array offset on value of type null 。

就是告訴你不能嘗試將 null,bool,int,float 或 resource 類型的值用作數(shù)組 ( 例如 $null[“key”] ) 會產(chǎn)生一個通知。

遇到的這個問題我是萬萬妹想到,搞了整整一下午,最后在公司大佬的幫助下半個小時幫我解決了問題。ps:第一個參數(shù)access_token那是輕輕松松(有問題可以看看和我的代碼哪里不同)

廢話不多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')){ //這一行是判斷公共方法有無這個方法,避免重名~ /** * PHP發(fā)送Json對象數(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)用接口代碼:(有空可以自己封裝一下~)

/**必須先進入登錄狀態(tài),然后拿到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 //請求電話號使用方法只能在公網(wǎng)能訪問的目錄下進行,本地進行沒有返回值 $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)獲取手機號失敗'); $phoneNumber = $phone[1]['phone_info']['phoneNumber']; /**拿到電話號碼end */

另外,thinkphp5獲取微信授權用戶手機號的相關實現(xiàn)方法,可參考前面一篇:https://www.jb51.net/article/229956.htm

標簽: PHP
相關文章:
主站蜘蛛池模板: 囊谦县| 邛崃市| 舒兰市| 大同市| 万宁市| 阜宁县| 正安县| 临猗县| 松阳县| 上虞市| 房产| 沙田区| 清新县| 饶阳县| 卢湾区| 湟源县| 湘潭市| 偃师市| 宣威市| 正镶白旗| 方正县| 航空| 黄大仙区| 合肥市| 社旗县| 信阳市| 慈溪市| 承德县| 昔阳县| 拜城县| 德保县| 广饶县| 新民市| 黔南| 桐庐县| 安宁市| 双流县| 鄂伦春自治旗| 龙海市| 通化市| 长兴县|