• uniapp微信小程序授权获得手机号


    1、uniapp前端

    1. //授权手机号
    2. decryptPhoneNumber (e) {
    3. let uids = uni.getStorageSync('UID');
    4. let data = {uid:uids,code:e.detail.code}
    5. manghe_phoen(data).then(res => {
    6. this.user = res.data;
    7. });
    8. console.log(e.detail.code)
    9. },

    2、php后台

    1. /**
    2. *授权手机号
    3. */
    4. public function manghe_phoen(Request $reques){
    5. $appid = '';
    6. $secret = '';
    7. $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$secret."";
    8. $ch = curl_init();
    9. curl_setopt($ch,CURLOPT_URL,$url);
    10. curl_setopt($ch,CURLOPT_HEADER,0);
    11. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
    12. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
    13. $res = curl_exec($ch);
    14. curl_close($ch);
    15. $tmptoken = json_decode($res);
    16. $token = $tmptoken->access_token;
    17. $uid = $reques->get("uid");
    18. $code = $reques->get("code");
    19. $url = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=$token";
    20. $data['code'] = $code;
    21. $date = $this->http_request($url,json_encode($data),'json');
    22. return json(['status'=>200,'data'=>$date,'msg'=>'ok']);
    23. }
    24. public function http_request($url, $data = null)
    25. {
    26. $curl = curl_init();
    27. curl_setopt($curl, CURLOPT_URL, $url);
    28. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
    29. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
    30. if (!empty($data)) {
    31. curl_setopt($curl, CURLOPT_POST, TRUE);
    32. curl_setopt($curl, CURLOPT_POSTFIELDS,$data);
    33. curl_setopt($curl, CURLOPT_HTTPHEADER, array(
    34. 'Content-Type: application/json'
    35. ));
    36. }
    37. curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
    38. $output = curl_exec($curl);
    39. curl_close($curl);
    40. return $output;
    41. exit();
    42. }

  • 相关阅读:
    MySQL锁
    新发布的Java使用率均超Java8
    设计模式6大原则
    zerotier充当网关实现内网互联,访问其它节点内网
    pthread_cancel手册翻译
    6.5 图的遍历
    【Linux】~ 如何将本地文件(windows)复制(传输)到虚拟机中
    【力扣-每日一题】213. 打家劫舍 II
    Compose学习-> Text()
    GIT高级使用技巧
  • 原文地址:https://blog.csdn.net/qq_34631220/article/details/126276188