欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

PHP配合微信小程序?qū)崿F(xiàn)獲取手機(jī)號(hào)碼詳解

 更新時(shí)間:2022年08月19日 11:55:51   作者:黃啊碼  
這篇文章主要為大家詳細(xì)介紹了PHP如何配合微信小程序?qū)崿F(xiàn)獲取手機(jī)號(hào)碼功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下

今天剛好做項(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)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論