QQ的账号登录及api操作,使用oauth 2.0
官方提供的sdk都太过庞大,这是我自己简化的,提供简单的账号登录、获取个人信息、发布分享等功能,如果需要其他功能可以根据官方的api文档自行添加
[文件] qq.php
003 |
* PHP Library for qq.com |
005 |
* @author php100(http://www.php100.com/) |
009 |
function __construct($appid, $appkey, $access_token=NULL){ |
011 |
$this->appkey=$appkey; |
012 |
$this->access_token=$access_token; |
015 |
function login_url($callback_url, $scope=''){ |
017 |
'client_id'=>$this->appid, |
018 |
'redirect_uri'=>$callback_url, |
019 |
'response_type'=>'code', |
022 |
return 'https://graph.qq.com/oauth2.0/authorize?'.http_build_query($params); |
025 |
function access_token($callback_url, $code){ |
027 |
'grant_type'=>'authorization_code', |
028 |
'client_id'=>$this->appid, |
029 |
'client_secret'=>$this->appkey, |
032 |
'redirect_uri'=>$callback_url |
034 |
$url='https://graph.qq.com/oauth2.0/token?'.http_build_query($params); |
035 |
$result_str=$this->http($url); |
037 |
if($result_str!='')parse_str($result_str, $json_r); |
042 |
function access_token_refresh($refresh_token){ |
046 |
function get_openid(){ |
048 |
'access_token'=>$this->access_token |
050 |
$url='https://graph.qq.com/oauth2.0/me?'.http_build_query($params); |
051 |
$result_str=$this->http($url); |
054 |
preg_match('/callback\(\s+(.*?)\s+\)/i', $result_str, $result_a); |
055 |
$json_r=json_decode($result_a[1], true); |
060 |
function get_user_info($openid){ |
064 |
$url='https://graph.qq.com/user/get_user_info'; |
065 |
return $this->api($url, $params); |
068 |
function add_share($openid, $title, $url, $site, $fromurl, $images='', $summary=''){ |
078 |
$url='https://graph.qq.com/share/add_share'; |
079 |
return $this->api($url, $params, 'POST'); |
082 |
function api($url, $params, $method='GET'){ |
083 |
$params['access_token']=$this->access_token; |
084 |
$params['oauth_consumer_key']=$this->appid; |
085 |
$params['format']='json'; |
087 |
$result_str=$this->http($url.'?'.http_build_query($params)); |
089 |
$result_str=$this->http($url, http_build_query($params), 'POST'); |
092 |
if($result_str!='')$result=json_decode($result_str, true); |
096 |
function http($url, $postfields='', $method='GET', $headers=array()){ |
098 |
curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, FALSE); |
099 |
curl_setopt($ci, CURLOPT_RETURNTRANSFER, 1); |
100 |
curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, 30); |
101 |
curl_setopt($ci, CURLOPT_TIMEOUT, 30); |
103 |
curl_setopt($ci, CURLOPT_POST, TRUE); |
104 |
if($postfields!='')curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields); |
106 |
$headers[]="User-Agent: qqPHP(piscdong.com)"; |
107 |
curl_setopt($ci, CURLOPT_HTTPHEADER, $headers); |
108 |
curl_setopt($ci, CURLOPT_URL, $url); |
109 |
$response=curl_exec($ci); |
[文件] config.php
3 |
header('Content-Type: text/html; charset=UTF-8'); |
6 |
$qq_s=''; //QQ应用APP KEY |
7 |
$callback_url='http://yoururl/callback.php'; //授权回调网址 |
8 |
$scope='get_user_info,add_share'; //权限列表,具体权限请查看官方的api文档 |
[文件] index.php
03 |
require_once('config.php'); |
04 |
require_once('qq.php'); |
06 |
$qq_t=isset($_SESSION['qq_t'])?$_SESSION['qq_t']:''; |
10 |
$qq=new qqPHP($qq_k, $qq_s, $qq_t); |
11 |
$qq_oid=$qq->get_openid(); |
12 |
$openid=$qq_oid['openid']; //获取登录用户open id |
15 |
$result=$qq->get_user_info($openid); |
20 |
$title='开源中国'; //分享页面标题 |
21 |
$url='http://www.oschina.net/'; //分享页面网址 |
24 |
$result=$qq->add_share($openid, $title, $url, $site, $fromurl); |
30 |
$qq=new qqPHP($qq_k, $qq_s); |
31 |
$login_url=$qq->login_url($callback_url, $scope); |
32 |
echo '<a href="',$login_url,'">点击进入授权页面</a>'; |
[文件] callback.php
02 |
//授权回调页面,即配置文件中的$callback_url |
04 |
require_once('config.php'); |
05 |
require_once('qq.php'); |
07 |
if(isset($_GET['code']) && trim($_GET['code'])!=''){ |
08 |
$qq=new qqPHP($qq_k, $qq_s); |
09 |
$result=$qq->access_token($callback_url, $_GET['code']); |
11 |
if(isset($result['access_token']) && $result['access_token']!=''){ |
12 |
echo '授权完成,请记录<br/>access token:<input size="50" value="',$result['access_token'],'">'; |
14 |
//保存登录信息,此示例中使用session保存 |
15 |
$_SESSION['qq_t']=$result['access_token']; //access token |
19 |
echo '<br/><a href="./">返回</a>'; |