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代 ...
随机推荐
- Maven 实用命令和技巧
1.Jar冲突排查 maven dependency:tree 人工排除
- JavaScript——callback(回调函数
1.回调函数定义 回调函数就是一个通过函数指针调用的函数.如果你把函数的指针(地址)作为参数传递给另一个函数,当这个指针被用为调用它所指向的函数时,我们就说这是回调函数.回调函数不是由该函数的实现方直 ...
- C#之常见数组编码错误
摘抄自C#本质论(第四版,P55) 常见错误 错误描述 改正后的代码 int numbers[] 用于声明数组的方括号放在数据类型之后,而不是在变量标识符之后 int[] numbers; int[] ...
- Junit测试打印详细的log日志,可以看到sql
Junit测试打印详细的log日志,可以看到sql 在log4j.xml的日志配置文件中,把日志级别从info级别调整到debug级别: <?xml version="1.0" ...
- C# 读写十六进制bin 文件
读一个十六进制的bin文件,在bin文件添加四行头,生成新的bin文件.bin文件可以用vs打开查看. using System; using System.Collections.Generic; ...
- 多线程BackroundWorker 使用
参考文章:http://www.cnblogs.com/inforasc/archive/2009/10/12/1582110.html using System; using System.Coll ...
- FFmpeg-20160418-snapshot-bin
ESC 退出 0 进度条开关 1 屏幕原始大小 2 屏幕1/2大小 3 屏幕1/3大小 4 屏幕1/4大小 S 下一帧 [ -2秒 ] +2秒 ; -1秒 ' +1秒 下一个帧 -> -5秒 F ...
- 用 get 同步/异步 方式获取网络数据并输出
//同步请求 //创建NSString用来存储请求的网址 NSString* str=@"http://v.juhe.cn/weather/index?format=2&cityna ...
- C字符串和指针问题汇总
空指针和传参问题 1) 段错误.形参改为二级指针即可 void GetMemory( char *p ){ p = ( ); } void Test( void ){ char *str = NULL ...
- 初次使用 VUX
1. 因为以前没用过vux ,所以还是比较不熟练: 2.项目部署是根据git上的源码改的: 开始:将git上的项目下载或者clone到本地: 001:安装nodejs--> 002:npm in ...