第三方qq登陆

2019-05-16  本文已影响0人  七百年前
<?php

namespace App\Tools;

class Thirdlogin
{

    //高级功能-》开发者模式-》获取
    private $app_id       = '11'; //公众号appid
    private $app_secret   = '11'; //公众号app_secret 8fc0236b1fbddb0208d8487c97db1ceb
    private $redirect_uri = '/qqlogin'; //授权之后跳转地址
    private $scope        = 'get_user_info'; //授权之后跳转地址

    const GET_AUTH_CODE_URL    = "https://graph.qq.com/oauth2.0/authorize";
    const GET_ACCESS_TOKEN_URL = "https://graph.qq.com/oauth2.0/token";
    const GET_OPENID_URL       = "https://graph.qq.com/oauth2.0/me";

    /**
     * 获取登陆跳转
     * @return type
     */
    public function qq_login()
    {
        //-------生成唯一随机串防CSRF攻击
        $state          = md5(uniqid(rand(), true));
        $re['qq_state'] = $state;
        $redirect_uri   = urlencode($this->redirect_uri);
        $re['url']      = "https://graph.qq.com/oauth2.0/show?which=Login&display=pc&response_type=code&client_id={$this->app_id}&redirect_uri={$redirect_uri}&state={$state}&scope=get_user_info";
        return $re;
    }

    /**
     * 获取 access_token
     * @param type $code
     * @return array
     */
    public function qq_callback($code)
    {
        //-------请求参数列表
        $keysArr = array(
            "grant_type"    => "authorization_code",
            "client_id"     => $this->app_id,
            "redirect_uri"  => $this->redirect_uri,
            "client_secret" => $this->app_secret,
            "code"          => $code,
        );
        //------构造请求access_token的url
        $token_url = self::GET_ACCESS_TOKEN_URL . '?' . http_build_query($keysArr);
        $response  = $this->get_contents($token_url);
        if (strpos($response, "callback") !== false) {
            $lpos     = strpos($response, "(");
            $rpos     = strrpos($response, ")");
            $response = substr($response, $lpos + 1, $rpos - $lpos - 1);
            $msg      = json_decode($response);
            if (isset($msg->error)) {
                $this->showError($msg->error, $msg->error_description);
            }
        }
        $params = array();
        parse_str($response, $params);
        return $params["access_token"];
    }

    /**
     * curl 拼装
     * @param type $url
     * @return boolean
     */
    public function get_contents($url)
    {
        $arrContextOptions = array(
            "ssl" => array(
                "verify_peer"      => false,
                "verify_peer_name" => false,
            ),
        );
        if (ini_get("allow_url_fopen") == "1") {
            $response = file_get_contents($url, false,stream_context_create($arrContextOptions));
        } else {
            $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);
        }
        if (empty($response)) {
            return false;
        }
        return $response;
    }

    /**
     * 获取openid
     * @param type $access_token
     * @return type
     */
    public function get_openid($access_token)
    {
        //-------请求参数列表
        $keysArr = array(
            "access_token" => $access_token,
        );
        $graph_url = self::GET_OPENID_URL . '?' . http_build_query($keysArr);
        $response  = $this->get_contents($graph_url);
        //--------检测错误是否发生
        if (strpos($response, "callback") !== false) {
            $lpos     = strpos($response, "(");
            $rpos     = strrpos($response, ")");
            $response = substr($response, $lpos + 1, $rpos - $lpos - 1);
        }
        $user = json_decode($response);
        if (isset($user->error)) {
            $this->showError($user->error, $user->error_description);
        }
        return $user->openid;
    }

    /**
     * 获取用户信息
     * @param type $access_token
     * @param type $openid
     * @return type
     * string(810) "{
    "ret": 0,
    "msg": "",
    "is_lost":0,
    "nickname": "七百年前",
    "gender": "男",
    "province": "河南",
    "city": "开封",
    "year": "1990",
    "figureurl": "http:\/\/qzapp.qlogo.cn\/qzapp\/101466793\/167024B4CB665E3FF9B5857BA454D085\/30",
    "figureurl_1": "http:\/\/qzapp.qlogo.cn\/qzapp\/101466793\/167024B4CB665E3FF9B5857BA454D085\/50",
    "figureurl_2": "http:\/\/qzapp.qlogo.cn\/qzapp\/101466793\/167024B4CB665E3FF9B5857BA454D085\/100",
    "figureurl_qq_1": "http:\/\/thirdqq.qlogo.cn\/qqapp\/101466793\/167024B4CB665E3FF9B5857BA454D085\/40",
    "figureurl_qq_2": "http:\/\/thirdqq.qlogo.cn\/qqapp\/101466793\/167024B4CB665E3FF9B5857BA454D085\/100",
    "is_yellow_vip": "0",
    "vip": "0",
    "yellow_vip_level": "0",
    "level": "0",
    "is_yellow_year_vip": "0"
    }
    "
     */
    public function get_qq_userinfo($access_token, $openid)
    {
        //-------请求参数列表
        $keysArr = array(
            "access_token" => $access_token,
        );
        $graph_url = "https://graph.qq.com/user/get_user_info?access_token={$access_token}&oauth_consumer_key={$this->app_id}&openid={$openid}";
        $response  = $this->get_contents($graph_url);
        $user      = json_decode($response, true);
        return $user;
    }

    /**
     * showError
     * 显示错误信息
     * @param int $code 错误代码
     * @param string $description 描述信息(可选)
     */
    public function showError($code, $description = '$')
    {
        echo "<meta charset=\"UTF-8\">";
        echo "<h3>error:</h3>$code";
        echo "<h3>msg :</h3>$description";
        exit();
    }

    public function post($url, $keysArr, $flag = 0)
    {
        $ch = curl_init();
        if (!$flag) {
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        }

        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $keysArr);
        curl_setopt($ch, CURLOPT_URL, $url);
        $ret = curl_exec($ch);
        curl_close($ch);
        return $ret;
    }

}
上一篇下一篇

猜你喜欢

热点阅读