.qq.php
<?php
/**
* PHP Library for qq.com
*
* @author
*/
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;
}
}

.config.php
<?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文档
?> 
 .index.php
<?php
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>';
}
?>

 .callback.php
<?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>';
?>

QQ的账号登录及api操作的更多相关文章

  1. 腾讯微博的账号登录及api操作

    .tqq.php <?php /** * PHP Library for t.qq.com * * @author */ class tqqPHP { function __construct( ...

  2. 人人网的账号登录及api操作

    .renren.php <?php /** * PHP Library for renren.com * * @author */ class renrenPHP { function __co ...

  3. 新浪微博的账号登录及api操作

    .sina.php <?php /** * PHP Library for weibo.com * * @author */ class sinaPHP { function __constru ...

  4. 开心网的账号登录及api操作

    .kaixin.php <?php /** * PHP Library for kaixin001.com * * @author */ class kaixinPHP { function _ ...

  5. 豆瓣的账号登录及api操作

    .douban.php <?php /** * PHP Library for douban.com * * @author */ class doubanPHP { function __co ...

  6. 第三方账号登录--QQ登录,以及QQ微博账号登录

    在QQ登陆测试的时候,刚申请正常登陆,但是由于app未上线,或许是腾讯升级造成的个别时候QQ登陆无法成功会提示下图代码,功能上没啥问题,已经达到 测试效果了.附上腾讯错误代码图(大家测试QQ登陆的时候 ...

  7. QQ互联账号登录

    本文说明的是依据某应用通过网页的qq信息来登录的过程.用途是利用QQ账号就能高速自己主动注冊并可以登录客户应用. 从webserver与腾讯server通信获取开房平台用户OpenID,再在应用ser ...

  8. QQ,新浪,SNS等公众平台的登录及api操作

    QQ的写法地址:http://www.oschina.net/code/snippet_930167_19888 Sina的写法地址:http://www.oschina.net/code/snipp ...

  9. 腾讯QQAndroid API调用实例(QQ分享无需登录)

    腾讯QQAndroid API调用实例(QQ分享无需登录)   主要分为两个步骤: 配置Androidmanifest.xml 修改activity里边代码 具体修改如下:   1.Activity代 ...

随机推荐

  1. 新型序列化类库MessagePack,比JSON更快、更小的格式

    MessagePack is an efficient binary serialization format. It lets you exchange data among multiple la ...

  2. 【leetcode】Validate Binary Search Tree

    Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...

  3. 【leetcode】Minimum Path Sum

    Minimum Path Sum Given a m x n grid filled with non-negative numbers, find a path from top left to b ...

  4. 快速搭建VPN服务器

    http://www.ttlsa.com/linux/centos-install-pptp-vpn/

  5. 回调函数中使用MFC类的成员或对话框控件的简单方法

    在MFC的很多程序中,常常需要在回调函数中调用MFC类的类成员变量.类成员函数,亦或者对话框控件的句柄.由于回调函数是基于C编程的Windows SDK的技术,而类成员又有this指针客观条件限制.. ...

  6. cxLookupComboBox 控件

    cxLookupComboBox cxLookupComboBox1.Properties.ListSource        //显示数据源     dtsTmnList cxLookupCombo ...

  7. Java for LeetCode 215 Kth Largest Element in an Array

    Find the kth largest element in an unsorted array. Note that it is the kth largest element in the so ...

  8. json格式

    $.post('text.action',{....},function(datas){ var name=datas.data[0].name; }); 如果是多个还可以用循环获取.$.post(' ...

  9. python之集合,函数,装饰器

    本节主要内容如下: 1. set集合 2. 函数 -- 自定义函数 -- 内置函数 3. 装饰器 一. set 集合: 一个无序且不重复的序列. tuple算是list和str的杂合(杂交的都有自己的 ...

  10. ABAP 仓库理货单导出

    *&---------------------------------------------------------------------* *& Report   *& ...