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;
}
}