Created
April 10, 2013 14:04
-
-
Save phoenixg/5354934 to your computer and use it in GitHub Desktop.
原作者称:QQ的账号登录及api操作,使用oauth 2.0。官方提供的sdk都太过庞大,这是我自己简化的,提供简单的账号登录、获取个人信息、发布分享等功能,如果需要其他功能可以根据官方的api文档自行添加。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//授权回调页面,即配置文件中的$callback_url | |
session_start(); | |
require_once('config.php'); | |
require_once('qq.php'); | |
if(isset($_GET['code']) && trim($_GET['code'])!=''){ | |
$qq=new qqPHP($qq_k, $qq_s); | |
$result=$qq->access_token($callback_url, $_GET['code']); | |
} | |
if(isset($result['access_token']) && $result['access_token']!=''){ | |
echo '授权完成,请记录<br/>access token:<input size="50" value="',$result['access_token'],'">'; | |
//保存登录信息,此示例中使用session保存 | |
$_SESSION['qq_t']=$result['access_token']; //access token | |
}else{ | |
echo '授权失败'; | |
} | |
echo '<br/><a href="./">返回</a>'; | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//配置文件 | |
header('Content-Type: text/html; charset=UTF-8'); | |
$qq_k=''; //QQ应用APP ID | |
$qq_s=''; //QQ应用APP KEY | |
$callback_url='http://yoururl/callback.php'; //授权回调网址 | |
$scope='get_user_info,add_share'; //权限列表,具体权限请查看官方的api文档 | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// 转载自:http://www.oschina.net/code/snippet_930167_19888 ( QQ的账号登录及api操作 2013年04月03日 ) | |
// | |
session_start(); | |
require_once('config.php'); | |
require_once('qq.php'); | |
$qq_t=isset($_SESSION['qq_t'])?$_SESSION['qq_t']:''; | |
//检查是否已登录 | |
if($qq_t!=''){ | |
$qq=new qqPHP($qq_k, $qq_s, $qq_t); | |
$qq_oid=$qq->get_openid(); | |
$openid=$qq_oid['openid']; //获取登录用户open id | |
//获取登录用户信息 | |
$result=$qq->get_user_info($openid); | |
var_dump($result); | |
/** | |
//发布分享 | |
$title='开源中国'; //分享页面标题 | |
$url='http://www.oschina.net/'; //分享页面网址 | |
$site=''; //QQ应用名称 | |
$fromurl=''; //QQ应用网址 | |
$result=$qq->add_share($openid, $title, $url, $site, $fromurl); | |
var_dump($result); | |
**/ | |
}else{ | |
//生成登录链接 | |
$qq=new qqPHP($qq_k, $qq_s); | |
$login_url=$qq->login_url($callback_url, $scope); | |
echo '<a href="',$login_url,'">点击进入授权页面</a>'; | |
} | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* PHP Library for qq.com | |
* | |
* @author PiscDong (http://www.piscdong.com/) | |
*/ | |
class qqPHP | |
{ | |
function __construct($appid, $appkey, $access_token=NULL){ | |
$this->appid=$appid; | |
$this->appkey=$appkey; | |
$this->access_token=$access_token; | |
} | |
function login_url($callback_url, $scope=''){ | |
$params=array( | |
'client_id'=>$this->appid, | |
'redirect_uri'=>$callback_url, | |
'response_type'=>'code', | |
'scope'=>$scope | |
); | |
return 'https://graph.qq.com/oauth2.0/authorize?'.http_build_query($params); | |
} | |
function access_token($callback_url, $code){ | |
$params=array( | |
'grant_type'=>'authorization_code', | |
'client_id'=>$this->appid, | |
'client_secret'=>$this->appkey, | |
'code'=>$code, | |
'state'=>'', | |
'redirect_uri'=>$callback_url | |
); | |
$url='https://graph.qq.com/oauth2.0/token?'.http_build_query($params); | |
$result_str=$this->http($url); | |
$json_r=array(); | |
if($result_str!='')parse_str($result_str, $json_r); | |
return $json_r; | |
} | |
/** | |
function access_token_refresh($refresh_token){ | |
} | |
**/ | |
function get_openid(){ | |
$params=array( | |
'access_token'=>$this->access_token | |
); | |
$url='https://graph.qq.com/oauth2.0/me?'.http_build_query($params); | |
$result_str=$this->http($url); | |
$json_r=array(); | |
if($result_str!=''){ | |
preg_match('/callback\(\s+(.*?)\s+\)/i', $result_str, $result_a); | |
$json_r=json_decode($result_a[1], true); | |
} | |
return $json_r; | |
} | |
function get_user_info($openid){ | |
$params=array( | |
'openid'=>$openid | |
); | |
$url='https://graph.qq.com/user/get_user_info'; | |
return $this->api($url, $params); | |
} | |
function add_share($openid, $title, $url, $site, $fromurl, $images='', $summary=''){ | |
$params=array( | |
'openid'=>$openid, | |
'title'=>$title, | |
'url'=>$url, | |
'site'=>$site, | |
'fromurl'=>$fromurl, | |
'images'=>$images, | |
'summary'=>$summary | |
); | |
$url='https://graph.qq.com/share/add_share'; | |
return $this->api($url, $params, 'POST'); | |
} | |
function api($url, $params, $method='GET'){ | |
$params['access_token']=$this->access_token; | |
$params['oauth_consumer_key']=$this->appid; | |
$params['format']='json'; | |
if($method=='GET'){ | |
$result_str=$this->http($url.'?'.http_build_query($params)); | |
}else{ | |
$result_str=$this->http($url, http_build_query($params), 'POST'); | |
} | |
$result=array(); | |
if($result_str!='')$result=json_decode($result_str, true); | |
return $result; | |
} | |
function http($url, $postfields='', $method='GET', $headers=array()){ | |
$ci=curl_init(); | |
curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, FALSE); | |
curl_setopt($ci, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, 30); | |
curl_setopt($ci, CURLOPT_TIMEOUT, 30); | |
if($method=='POST'){ | |
curl_setopt($ci, CURLOPT_POST, TRUE); | |
if($postfields!='')curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields); | |
} | |
$headers[]="User-Agent: qqPHP(piscdong.com)"; | |
curl_setopt($ci, CURLOPT_HTTPHEADER, $headers); | |
curl_setopt($ci, CURLOPT_URL, $url); | |
$response=curl_exec($ci); | |
curl_close($ci); | |
return $response; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment