PHP配合微信小程序?qū)崿F(xiàn)獲取手機(jī)號(hào)碼詳解
今天剛好做項(xiàng)目的時(shí)候用到這塊功能,黃啊碼就直接上手了,奈何網(wǎng)上的教程各式各樣,就是沒有個(gè)直接可以抄的,啊碼最煩說話說一半,今天就直接弄個(gè)給大家抄的。
當(dāng)前通過獲取session_key與encryptedData與iv進(jìn)行解密獲取手機(jī)號(hào)的方法已經(jīng)不行了,只能通過點(diǎn)擊按鈕來實(shí)現(xiàn)獲取微信用戶的手機(jī)號(hào)
1:需要將 button 組件 open-type 的值設(shè)置為 getPhoneNumber,當(dāng)用戶點(diǎn)擊并同意之后,可以通過 bindgetphonenumber 事件回調(diào)獲取到動(dòng)態(tài)令牌code,然后把code傳到開發(fā)者后臺(tái),并在開發(fā)者后臺(tái)調(diào)用微信后臺(tái)提供的 phonenumber.getPhoneNumber 接口,消費(fèi)code來換取用戶手機(jī)號(hào)。每個(gè)code有效期為5分鐘,且只能消費(fèi)一次。
注:getPhoneNumber 返回的 code 與 wx.login 返回的 code 作用是不一樣的,不能混用。
代碼如下:
wxss代碼:
<button type="primary" style="width:100%" bindgetphonenumber="onGetPhoneNumber" open-type="getPhoneNumber">獲取</button>
js代碼:
onGetPhoneNumber (e){ if(e.detail.code==null||e.detail.code==""){ wx.showToast({ title: '請(qǐng)?jiān)试S獲取您的手機(jī)號(hào)', 'icon':'none', }) return; }else{ wx.request({ data: { code: e.detail.code, time:config.dt, openid: storage.get('openid') }, header: {'content-type': 'application/json'}, url: config.api+'/getWxPhone', success: function(res) { console.log(res.data.data.phone); } }) } },
2:后端PHP代碼【此處我用的是tp5】根據(jù)傳過來的動(dòng)態(tài)令牌code去獲取手機(jī)號(hào)
/** * @param Request $request * 獲取手機(jī)號(hào)碼 */ public function getWxPhone(Request $request){ $params = $request::only(['code']); $url_get = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.config("appid").'&secret='.config("appsecret"); $tmptoken = json_decode(curlGet($url_get),true); $token = $tmptoken['access_token']; $url = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=".$token; $data['code']=$params['code']; $info = Post(json_encode($data),$url); $tmpinfo = json_decode($info,true); $code = $tmpinfo['errcode']; $phoneNumber = ""; $phoneNumber = $tmpinfo['phone_info']['phoneNumber']; if($code == '0'){ self::returnMsg(Error::SUCCESS, '獲取手機(jī)號(hào)碼成功',['phone'=>$phoneNumber]); }else{ self::returnMsg(Error::FAILED, '獲取手機(jī)號(hào)碼失敗',['']); } }
附帶函數(shù):
function Post($curlPost, $url, $ssl = false) { ? ? $curl = curl_init(); ? ? curl_setopt($curl, CURLOPT_URL, $url); ? ? curl_setopt($curl, CURLOPT_HEADER, false); ? ? curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); ? ? curl_setopt($curl, CURLOPT_NOBODY, true); ? ? curl_setopt($curl, CURLOPT_POST, true); ? ? curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost); ? ? if (!$ssl) { ? ? ? ? curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); ? ? ? ? curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); ? ? } ? ? $return_str = curl_exec($curl); ? ? curl_close($curl); ? ? return $return_str; }
可能出現(xiàn)的錯(cuò)誤:errcode“:47001
問題所在:
這里肯定是忘記用json_encode
除了這個(gè)問題,某些大聰明娃喜歡把
https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=".$token
這里的access_token變成data里邊的參數(shù),這時(shí)候就出現(xiàn)了access_token過期的問題。
一切問題來源于沒有好好看官方文檔
因?yàn)槟闳绻麑ccess_token當(dāng)做參數(shù),接口就變成了用兩次access_token,第一次木有問題,第二次就只能跟你說拜拜了(access_token過期或無效)。
到此這篇關(guān)于PHP配合微信小程序?qū)崿F(xiàn)獲取手機(jī)號(hào)碼詳解的文章就介紹到這了,更多相關(guān)PHP獲取手機(jī)號(hào)碼內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- uniapp抖音小程序一鍵獲取用戶手機(jī)號(hào)的示例代碼
- UNIAPP實(shí)現(xiàn)微信小程序登錄授權(quán)和手機(jī)號(hào)授權(quán)功能(uniapp做微信小程序)
- 微信小程序獲取用戶手機(jī)號(hào)碼詳細(xì)教程(前端+后端)
- 微信小程序中獲取用戶手機(jī)號(hào)授權(quán)登錄詳細(xì)步驟
- uniapp微信小程序授權(quán)登錄并獲取手機(jī)號(hào)的方法
- uniapp+.net?core實(shí)現(xiàn)微信小程序獲取手機(jī)號(hào)功能
- 微信小程序登錄方法之授權(quán)登陸及獲取微信用戶手機(jī)號(hào)
- 微信小程序獲取用戶手機(jī)號(hào)碼的詳細(xì)步驟
- 微信小程序?qū)崿F(xiàn)手機(jī)號(hào)碼驗(yàn)證
- 抖音小程序一鍵獲取手機(jī)號(hào)的實(shí)現(xiàn)思路
相關(guān)文章
PHP面向?qū)ο蟪绦蛟O(shè)計(jì)之對(duì)象克隆clone和魔術(shù)方法__clone()用法分析
這篇文章主要介紹了PHP面向?qū)ο蟪绦蛟O(shè)計(jì)之對(duì)象克隆clone和魔術(shù)方法__clone()用法,結(jié)合具體實(shí)例形式分析了php面向?qū)ο蟪绦蛟O(shè)計(jì)中對(duì)象克隆clone和魔術(shù)方法__clone()相關(guān)操作技巧與使用注意事項(xiàng),需要的朋友可以參考下2019-06-06php curl 登錄163郵箱并抓取郵箱好友列表的代碼(經(jīng)測(cè)試)
PHP模擬登陸獲取163郵箱聯(lián)系人的實(shí)現(xiàn)代碼,需要的朋友可以參考下。2011-04-04gearman中任務(wù)的優(yōu)先級(jí)和返回狀態(tài)實(shí)例分析
這篇文章主要介紹了gearman中任務(wù)的優(yōu)先級(jí)和返回狀態(tài),結(jié)合實(shí)例形式分析了gearman任務(wù)的優(yōu)先級(jí)以及獲取返回狀態(tài)相關(guān)操作技巧,需要的朋友可以參考下2020-02-02談?wù)凱HP連接Access數(shù)據(jù)庫的注意事項(xiàng)
有的時(shí)候需要用php連接access數(shù)據(jù)庫,結(jié)果整了半天Access數(shù)據(jù)庫就是連接不上,查找很多資料,以下是些個(gè)人經(jīng)驗(yàn),希望能給需要連接access 數(shù)據(jù)的人帶來幫助。2016-08-08