<?php
// +----------------------------------------------------------------------
// | QQ登录类 [ 天方夜谭快速开发框架 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2014~2019 http://www.eirds.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 天方夜谭 <17404785@qq.com>
// +----------------------------------------------------------------------

class qqlogin{
    //定义参数
    public $api = 'qqlogin.eirds.com';
    //初始化
    public function __construct($access_token=null){
        $this->access_token=$access_token;
    }
    //转跳到QQ登录(用于用户登录)
    public function url(){
        return 'http://'.$this->api.'/qqlogin.php?return_url=http://'.$_SERVER['HTTP_HOST'].'/Application/Users.php?Mould=QQLogin';
    }
    //转跳到QQ登录2(用于绑定QQ)
    public function url2(){
        return 'http://'.$this->api.'/qqlogin.php?return_url=http://'.$_SERVER['HTTP_HOST'].'/Application/Mains.php?Mould=User';
    }
    //获取get_auth_info信息
    public function get_auth_info(){
        $data = $this->get_contents('http://'.$this->api.'/API/get_auth_info.php?access_token='.$this->access_token);
        preg_match('/callback\((.*?)\)\;/is',$data,$json);
        $arr = json_decode($json[1],true);
        return $arr;
    }
    //获取获取get_user_info信息
    public function get_user_info(){
        $json = $this->get_contents('http://'.$this->api.'/API/get_user_info.php?access_token='.$this->access_token.'&oauth_consumer_key='.$this->get_auth_info()['client_id'].'&openid='.$this->get_auth_info()['openid']);
        $arr = json_decode($json,true);
        return $arr;
    }
    /**
     * get_contents
     * 服务器通过get请求获得内容
     * @param string $url       请求的url,拼接后的
     * @return string           请求返回的内容
     */
    public function get_contents($url){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_URL, $url);
        $response =  curl_exec($ch);
        curl_close($ch);
        return $response;
    }
}