TP5.0 小程序用户无需登录,直接获取用户手机号。

<?php

namespace app\api\controller;


use app\common\controller\Api;
use app\common\model\User;
use think\Hook;
use think\Db;
use think\Cache;

/**
 *
 */
class Wechat extends Api
{


    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    public $AppId = '填你自己的';
    public $Secret = '填你自己的';



    //获取用户手机号
    public function user_phone(){
        //获取前端传过来的code,如果前端不知道code是啥,就刁他。
      $post = $this->request->param();
        if(!isset($post['code']) || empty($post['code'])){
            $return['status'] = 222;
            $return['msg'] = '非法请求';
            return json_encode($return);
            
        }
        
        
      //获取accesstoken
        
       $accessToken = $this->get_access_token();
      
    //请求地址
       $url = 'https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token='.$accessToken;
     //前端传递的code
       $code = $post['code'];
       //组成数组
       $data=[
            'code'=>$code,
        ];

          //这里要把传递的参数给转成json,不然小程序接口会报数据类型错误。
        $result = json_decode($this->curl_post_https($url,json_encode($data)),true);
        //开始判断获取是否成功
        if($result['errmsg'] == 0){
            //获取成功
            $phoen = $result['phone_info']['phoneNumber'];
            $return['smg'] = '获取手机号成功!';
            $return['code'] = 200;
            $return['phone'] = $phoen;
            //把手机号返回给前端,或者自己进行存储。看需求
            //Db::name('user')->add();
            return json_encode($return);
            
        }else{
            $return['smg'] = '获取手机号失败!';
            $return['code'] = 201;
            return json_encode($return);
            
        }

      
    }


     //获取小程序二维码的token
    public function get_access_token()
    {
        //先判断缓存里面的access_token过期了没有
        if(Cache::get('access_token')){
            //没过期直接拿出来用
            $a = Cache::get('access_token');
            return $a;
        }else{
            //过期了就重新获取
            $appid = $this->AppId;
            $secret = $this->Secret;
            $url = "https://api.weixin.qq.com/cgi-bin/token? grant_type=client_credential&appid=$appid&secret=$secret";
            //请求接口,获取accesstoken
            $user_obj = $this->curlHttp($url);
            //然后将accesstoken存入缓存里面,官方过期时间7200秒,缓存里面可以过期的早一点,自己把控
            Cache::set('access_token',$user_obj['access_token'],7100);
            
            return Cache::get('access_token');
        }
    }


    
       /**
     * 发送get请求
     */
    public function curlHttp($url){


        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,$url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        $output = curl_exec($ch);
        //释放curl句柄
        curl_close($ch);
        return json_decode($output,true);
    }

           /**
     * 发送post请求
     */
    public function curl_post_https($url,$data,$header=[]){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_HEADER, $header);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        $output = curl_exec($ch);
        curl_close($ch);

        return $output;
    }

}

请求成功之后返回的数据。

 

 非常简单!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值