• 小程序+Php获取微信手机号


    PHP怎么配合微信小程序实现获取手机号码

    当前通过获取session_key与encryptedData与iv进行解密获取手机号的方法已经不行了,只能通过点击按钮来实现获取微信用户的手机号

    1:需要将 button 组件 open-type 的值设置为 getPhoneNumber,当用户点击并同意之后,可以通过 bindgetphonenumber 事件回调获取到动态令牌code,然后把code传到开发者后台,并在开发者后台调用微信后台提供的 phonenumber.getPhoneNumber 接口,消费code来换取用户手机号。每个code有效期为5分钟,且只能消费一次。

    注:getPhoneNumber 返回的 code 与 wx.login 返回的 code 作用是不一样的,不能混用。

    代码如下:

    wxss代码:

     <button type="primary"  bindgetphonenumber="onGetPhoneNumber" open-type="getPhoneNumber">获取</button>

    复制

    js代码:

    1. onGetPhoneNumber (e){
    2. if(e.detail.code==null||e.detail.code==""){
    3. wx.showToast({
    4. title: '请允许获取您的手机号',
    5. 'icon':'none',
    6. })
    7. return;
    8. }else{
    9. wx.request({
    10. data: {
    11. code: e.detail.code,
    12. time:config.dt,
    13. openid: storage.get('openid')
    14. },
    15. header: {'content-type': 'application/json'},
    16. url: config.api+'/getWxPhone',
    17. success: function(res) {
    18. console.log(res.data.data.phone);
    19. }
    20. })
    21. }
    22. },

    复制

    2:后端PHP代码【此处我用的是tp5】根据传过来的动态令牌code去获取手机号

    1. /**
    2. * @param Request $request
    3. * 获取手机号码
    4. */
    5. public function getWxPhone(Request $request){
    6. $params = $request::only(['code']);
    7. $url_get = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.config("appid").'&secret='.config("appsecret");
    8. $tmptoken = json_decode(curlGet($url_get),true);
    9. $token = $tmptoken['access_token'];
    10. $url = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=".$token;
    11. $data['code']=$params['code'];
    12. $info = Post(json_encode($data),$url);
    13. $tmpinfo = json_decode($info,true);
    14. $code = $tmpinfo['errcode'];
    15. $phoneNumber = "";
    16. $phoneNumber = $tmpinfo['phone_info']['phoneNumber'];
    17. if($code == '0'){
    18. self::returnMsg(Error::SUCCESS, '获取手机号码成功',['phone'=>$phoneNumber]);
    19. }else{
    20. self::returnMsg(Error::FAILED, '获取手机号码失败',['']);
    21. }
    22. }

    复制

    附带函数:

    1. function Post($curlPost, $url, $ssl = false)
    2. {
    3. $curl = curl_init();
    4. curl_setopt($curl, CURLOPT_URL, $url);
    5. curl_setopt($curl, CURLOPT_HEADER, false);
    6. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    7. curl_setopt($curl, CURLOPT_NOBODY, true);
    8. curl_setopt($curl, CURLOPT_POST, true);
    9. curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);
    10. if (!$ssl) {
    11. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    12. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
    13. }
    14. $return_str = curl_exec($curl);
    15. curl_close($curl);
    16. return $return_str;
    17. }

    复制

    可能出现的错误:errcode“:47001

    问题所在:

    PHP怎么配合微信小程序实现获取手机号码

    这里肯定是忘记用json_encode

    除了这个问题,某些大聪明娃喜欢把

    https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=".$token 

    这里的access_token变成data里边的参数,这时候就出现了access_token过期的问题。

    一切问题来源于没有好好看官方文档

    因为你如果将access_token当做参数,接口就变成了用两次access_token(access_token过期或无效)。

  • 相关阅读:
    Nginx基础篇-Nginx 源码编译安装与平滑升级
    PHP-函数(定义,带缺省值,值传递、地址传递)php跨越+移植(include require include_once require_once)
    练习前端案例
    计算机毕业设计SSMjspm学科竞赛管理系统【附源码数据库】
    jmeter(二):jmeter组件总结,利用取样器中http发送请求
    数据库概论(简单介绍)
    晶振工作原理详解
    jsp528口腔牙医诊所预约挂号ssm+mysql Springboot
    Tomcat
    引入Wukong让你的系统瞬间具备IOC能力
  • 原文地址:https://blog.csdn.net/qq_35713752/article/details/133688548