QQ的账号登录及api操作
.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操作的更多相关文章
- 腾讯微博的账号登录及api操作
.tqq.php <?php /** * PHP Library for t.qq.com * * @author */ class tqqPHP { function __construct( ...
- 人人网的账号登录及api操作
.renren.php <?php /** * PHP Library for renren.com * * @author */ class renrenPHP { function __co ...
- 新浪微博的账号登录及api操作
.sina.php <?php /** * PHP Library for weibo.com * * @author */ class sinaPHP { function __constru ...
- 开心网的账号登录及api操作
.kaixin.php <?php /** * PHP Library for kaixin001.com * * @author */ class kaixinPHP { function _ ...
- 豆瓣的账号登录及api操作
.douban.php <?php /** * PHP Library for douban.com * * @author */ class doubanPHP { function __co ...
- 第三方账号登录--QQ登录,以及QQ微博账号登录
在QQ登陆测试的时候,刚申请正常登陆,但是由于app未上线,或许是腾讯升级造成的个别时候QQ登陆无法成功会提示下图代码,功能上没啥问题,已经达到 测试效果了.附上腾讯错误代码图(大家测试QQ登陆的时候 ...
- QQ互联账号登录
本文说明的是依据某应用通过网页的qq信息来登录的过程.用途是利用QQ账号就能高速自己主动注冊并可以登录客户应用. 从webserver与腾讯server通信获取开房平台用户OpenID,再在应用ser ...
- QQ,新浪,SNS等公众平台的登录及api操作
QQ的写法地址:http://www.oschina.net/code/snippet_930167_19888 Sina的写法地址:http://www.oschina.net/code/snipp ...
- 腾讯QQAndroid API调用实例(QQ分享无需登录)
腾讯QQAndroid API调用实例(QQ分享无需登录) 主要分为两个步骤: 配置Androidmanifest.xml 修改activity里边代码 具体修改如下: 1.Activity代 ...
随机推荐
- Spring框架学习[IoC容器高级特性]
1.通过前面4篇文章对Spring IoC容器的源码分析,我们已经基本上了解了Spring IoC容器对Bean定义资源的定位.读入和解析过程,同时也清楚了当用户通过getBean方法向IoC容器获取 ...
- JS插件之——bootstrap-suggest.js
今天遇到了一个很牛逼的插件bootstrap-suggest.js 如此好用的搜索提示插件 简直酷毙了 源码下载地址 编写了一个例子,供以后参考 <!DOCTYPE HTML PUBLIC &q ...
- POJ 3009
http://poj.org/problem?id=3009 一个搜索的题目: 大意就是一个冰球,在冰面上滑动,你打击一次,就沿一个反向滑动,知道碰到墙就会停下,而墙则会破碎. 求从起点到终点的最短的 ...
- springMVC 访问404
问题:404 但是其他的controller可以访问!!!
- android SDK manager 无法获取更新版本列表
打开SDK Manager---Tools---Options,填入如下代理和端口,勾选选项也如下. 网址:mirrors.neusoft.edu.cn 端口:80 99%是成功的 参考:http:/ ...
- Unity3d《Shader篇》漫反射
Unity3d<Shader篇>漫反射 Shader "Custom/Ambient" { Properties { _MainTex ("Base (RGB ...
- 不知道数据库中表的列类型的前提下,使用JDBC正确的取出数据
概要: 使用jdbc 如果在不知道表结构的情况下,如何读出表信息? 使用ResultSetMetaData; 然后使用getColumnType 获取column 类型 使用getColumnName ...
- 17. javacript高级程序设计-错误处理与调试
1. 错误处理与调试 l 在可能发生错误的地方使用try-catch方法,可以对错误进行及时的相应 l 使用window.onerror事件处理程序,这种方式可以接受try-catch不能处理的所有错 ...
- NoSQL之【MongoDB】学习(二):DML和查询操作说明
摘要: 操作MongoDB的方法和关系型数据库差别很大,现在对他们进行说明,后期会逐步完善. ##开头表示MySQL** 开头表示MongoDB 创建: Mongodb:文档数据库,擅长存非结构化数据 ...
- 【转】Java-----jar反编译修改重新打包
原文链接:http://blog.csdn.net/hekewangzi/article/details/44676797 一.使用反编译工具JD-GUI(JD-GUI相关操作见Java-----反编 ...