Blogger Information
Blog 91
fans 2
comment 4
visits 127879
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
tp5之QQ互联
夏日的烈风的博客
Original
2127 people have browsed it

class Index extends Controller

{

    public function index() {


       return view();

    }

    //访问QQ登录页面

    public function qqLogin(){

        $oauth = new Oauth();

    $oauth->qq_login();

    }

 

    //qq回调函数

    public function qqCallback(){

    //请求accesstoken

       // Step2:获取Authorization Code

        $code = $_GET['code'];


        // Step3:通过Authorization Code获取Access Token

        $url = 'https://graph.qq.com/oauth2.0/token?grant_type=authorization_code&client_id=101434622&client_secret=2440d1055d7ec8857a659e1304537bce&code='.$code.'&redirect_uri==http://huangzhuquan.cn/index.php?index/login/qqlogin.html';

        $res = https_request($url);

        // 返回的数据

        // access_token=YOUR_ACCESS_TOKEN&expires_in=3600

        // 切割字符串 获取access_token

        $res = explode('&', $res);

        $res = explode('=',$res[0]);

        $access_token = $res[1];


        // Step4:使用Access Token来获取用户的OpenID

        $openid_url = 'https://graph.qq.com/oauth2.0/me?access_token='.$access_token;

        $openid = https_request($openid_url);

        $rep = array("callback(",");\n");

        $jsonopenid = str_replace($rep, "", $openid);

        // $josnopenid 用户id 

        $josnopenid = json_decode($jsonopenid);


        // 获取返回包 获取openid

        if (strpos($openid, "callback") !== false)

        {

        $lpos = strpos($openid, "(");

        $rpos = strrpos($openid, ")");

        $openid = substr($openid, $lpos + 1, $rpos - $lpos -1);

        }

        $user = json_decode($openid,true);

        // Step5:使用Access Token以及OpenID来访问和修改用户数据

        $userinfo_url = 'https://graph.qq.com/user/get_user_info?access_token='.$access_token.'&oauth_consumer_key=101434622&openid='.$openid_info;

        $userinfo = https_request($userinfo_url);


        // uinfo 用户信息对象

        $uinfo = json_decode($userinfo);

       file_put_contents('./1.txt', $uinfo);

        $arr = [$josnopenid->openid, $uinfo->nickname, $uinfo->gender, $uinfo->figureurl_2];

         // 修改信息

        $sex = ($uinfo->gender=='男')?'1':'0';

        // 查看id是否登记过

        $findid = DB::name('user')->where('openid',$josnopenid->openid)->select();

        if($findid){

            // 找得到该id,直接登录

            Session::set('index_name',$uinfo->nickname);

            Session::set('index_id',$findid[0]['id']);

            return $this->redirect('index/index/index');

        }else{

            // 注册

            $insertid = DB::name('user')->insertGetId(['openid'=>$josnopenid->openid, 'name'=>$uinfo->nickname, 'status'=>1]);

            $insertinfo = DB::name('user_info')->insertGetId(['sex'=>$sex, 'photo'=>'default.jpg', 'addtime'=>time()]);

            $ui_id = DB::name('ui_id')->insert(['user_id'=>$insertid, 'user_info_id'=>$insertinfo]);

            Session::set('index_name',$uinfo->nickname);

            Session::set('index_id',$insertid);

            return $this->redirect('index/index/index');

        }

    }

 




    // 这里是封装的curl 的 get 和post 方法 qq登录

    function https_request($url,$data)

    {

        // curl 初始化

        $ch = curl_init();


        // 参数设置 

        curl_setopt($ch,CURLOPT_URL,$url);

        curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);

        curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE);


        // 这里进行 判断

        if ( !empty($data) ) {

        curl_setopt($ch, CURLOPT_POST, 1);

        curl_setopt($ch,CURLOPT_POSTFIELDS,$data);


        }

        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

        // 执行

        $res = curl_exec($ch);

        // curl_close($ch);

        return $res;

    }

}

Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post