tp5微信网页授权接口开发,获取微信用户信息

use think\Controller;
use think\Db;
class Hd extends Controller
{
    private $appid  = "wx47d06f01aca201f5";   //你的appId
    private $secret = "50263c6adbbe9f23a2cb7d721de28985";   //你的appSecret

    //获取用户的openid
    function index(){
        //1.获取到code=================================
        $redirect_uri = urlencode("http://".$_SERVER['HTTP_HOST']."/index/hd/getUserInfo");//这里的地址需要http://
        $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$this->appid."&redirect_uri=".$redirect_uri."&response_type=code&scope=snsapi_base&state=123#wechat_redirect";
        $this->redirect($url);
        exit();
    }
    //通过post获取用户的信息
    function getJson($url){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($ch);
        curl_close($ch);
        return json_decode($output, true);
    }
    //获取用户的openid
    function getUserInfo(){
        $data = request()->get();
        $code   = $data['code'];
        //2.获取到网页授权的access_token===============
        //第一步,获取access_token
        //新建一个文件存储access_token,避免多次获取,access_token的有效时间是2个小时,这里的目录是根目录:www.xxx.xxx/access_token.txt
        $file_name = 'access_token.txt';
        if (!file_exists($file_name)) {     //文件不存在,新建并附赋权限
            $my_file = fopen($file_name, "w");
            $file_name = $my_file;
            chmod($file_name,0777);
        }
        $content = file_get_contents($file_name);  //读取文件内容
        $arr = json_decode($content,true);  //转化为数组
        if (is_array($arr)) {
            if (isset($arr['end_time']) && $arr['end_time'] > time()) {  //access_token未过时,直接拿来使用
                $token = $arr;
            } else {    //access_token超时,重新获取
                $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$this->appid&secret=$this->secret";
                $token = $this->getJson($url);
                $token['start_time'] = time();
                $token['end_time']   = time()+7000;
                $my_file = fopen($file_name, "w") or die("Unable to open file!");
                $txt = json_encode($token);
                fwrite($my_file, $txt);
                fclose($my_file);
            }
        } else {     //用于新建文件时的写入
            $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$this->appid&secret=$this->secret";
            $token = $this->getJson($url);
            $token['start_time'] = time();
            $token['end_time']   = time()+7000;
            $my_file = fopen($file_name, "w") or die("Unable to open file!");
            $txt = json_encode($token);
            fwrite($my_file, $txt);
            fclose($my_file);
        }
        //第二步:取得openid
        $oauth2Url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=$this->appid&secret=$this->secret&code=$code&grant_type=authorization_code";
        $oauth2 = $this->getJson($oauth2Url);
        $openid = $oauth2['openid'];
        //第三步:根据全局access_token和openid查询用户信息
        $access_token = $token["access_token"];
        $openid = $oauth2['openid'];
        $get_user_info_url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=$access_token&openid=$openid&lang=zh_CN";
        $user_info = $this->getJson($get_user_info_url);
        $user_info=json_encode($user_info);
        $this->redirect('rouge','user_info='.$user_info);
    }
    //活动主体
    public function rouge(){
        $user=input('user_info');
        $user=json_decode($user);
        return view('',[
            'title'=>"活动",
            'user_info'=>$user,
        ]);
    }

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值