深圳网站建设-深圳做网站

php实现QQ登录API接口代码

来源:深圳网站建设 发布时间:2/8/2015 8:50:25 AM 浏览次数:

本代码可为你的网站添加上QQ号登录的功能,调用腾讯的API接口,这只是一个接口的代码,要想实现真正的登录,还需要自己添加部分代码调用,有兴趣的朋友可拿去参考或使用,以下是深圳网站建设提供详细的PHP代码。

<?php
if($_REQUEST['state'] == $_SESSION['state']){
	$token_url = "https://graph.qq.com/oauth2.0/token?grant_type=authorization_code&". "client_id=" . C('L.QQappid'). "&redirect_uri=" . urlencode(str_replace('__APP__/','/',C('L.QQcallback'))). "&client_secret=" . C('L.QQappkey'). "&code=" . $_REQUEST["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);
	}
	$params = array();
	parse_str($response, $params);
	$_SESSION["access_token"] = $params["access_token"];
}
$graph_url = "https://graph.qq.com/oauth2.0/me?access_token=".$_SESSION['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);
$_SESSION["openid"] = $user->openid;
?>