php之qq登陆

qq登陆相对新浪登陆多一个步骤,不过qq登陆发送数据都是使用GET方式,所以之需要按照步骤把相应的url拼接下就可以了。

class qqClass{
    protected $appid = "";
    protected $callback = "";
    protected $appkey =  "";
    public function __construct()
    {
        require(dirname(__FILE__) . '/oauth.config.php');
        $this->appid    = $qq['appid'];
        $this->callback = urlencode($qq['callback']);
        $this->appkey   =  $qq['appkey'];
       
    }

//第一步获取code
    public function qq_login(){
        $response_type = "code";
         //-------生成唯一随机串防CSRF攻击
        $_SESSION['state'] = md5(uniqid(rand(), TRUE));


        $login_url =  "https://graph.qq.com/oauth2.0/authorize?response_type=".$response_type.
                      "&client_id=".$this->appid.
                      "&redirect_uri=".$this->callback.
                      "&state=".$_SESSION['state'];
        header("Location:$login_url");
    }

//第二步获取access_token
    public function qq_callback(){
        //--------验证state防止CSRF攻击
        
        if($_REQUEST['state'] == $_SESSION['state']){
           $grant_type = "authorization_code";
           $code = $_REQUEST['code'];


            //------构造请求access_token的url
            $token_url = "https://graph.qq.com/oauth2.0/token?grant_type=".$grant_type.
                         "&client_id=".$this->appid.
                         "&client_secret=".$this->appkey.
                         "&redirect_uri=".$this->callback.
                         "&code=".$code;
            $response = file_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))
                {
                    echo "<h3>error:</h3>" . $msg->error;
                    echo "<h3>msg  :</h3>" . $msg->error_description;
                    exit;
                }
            }


            $params = array();
            parse_str($response, $params);
            return $params["access_token"];
        }
        else
        {
             return "";
        }
    }

//第二步获取openid
    public function get_openid($access_token){
        if(!empty($access_token))
        {
            $graph_url = "https://graph.qq.com/oauth2.0/me?access_token=".$access_token;


            $str  = file_get_contents($graph_url);
            if (strpos($str, "callback") !== false)
            {
                $lpos = strpos($str, "(");
                $rpos = strrpos($str, ")");
                $str  = substr($str, $lpos + 1, $rpos - $lpos -1);
            }


            $user = json_decode($str);
            if (isset($user->error))
            {
                echo "<h3>error:</h3>" . $user->error;
                echo "<h3>msg  :</h3>" . $user->error_description;
                exit;
            }
            return $user->openid;
        }else
        {
            return "";
        }


       


    }


    function get_user_info($access_token, $openid)
    {
       
        if(!empty($access_token) && !empty($openid))
        {
            $user_url = "https://graph.qq.com/user/get_user_info?access_token=".$access_token.
                    "&oauth_consumer_key=".$this->appid.
                    "&openid=".$openid.
                    "&format=json";
            $response = file_get_contents($user_url);
            $arr = json_decode($response, true);
            return $arr;
        }else
        {
            return "";
        }
        
    }


    
}


oaulogin.php

if($type == "qqlogin")
{
require(dirname(__FILE__) . '/oauth/qq.class.php');
$qc = new qqClass();
$qc->qq_login();
}

elseif($type == "qq_callback")
{
require(dirname(__FILE__) . '/oauth/qq.class.php');
$qc = new qqClass();
$access_token = $qc->qq_callback();
$openid = $qc->get_openid($access_token);
$arr = $qc->get_user_info($access_token, $openid);
if(empty($arr))
{
show_message("登陆错误","首页","index.php");
}
//根据openid判断用户是否第一次登陆
$have_openid = $db->getRow("xxxxxxxx WHERE openid=.$openid);


if(!empty($have_openid))
{
//更新用户信息
$_SESSION['user_id'] = $have_openid['user_id'];
$_SESSION['user_name'] = $have_openid['user_name'];
update_user_info();
        ecs_header("Location: index.php\n");
        exit;
}else
{
//插入用户信息
$email = ;
$user_name = ];
$sex = ;
$password=;

otherinsert($email, $user_name, $sex, $password, .$openid);
ecs_header("Location: user.php\n");
        exit;
}




}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值