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代 ...
随机推荐
- PL/Cool
毛子 2003 Petrozavodsk, Final Contest, 8.30.03. G. PL/Cool 实现一个程序,使它读入一段PL/Cool程序,并输出它的结果. PL/Cool语法 b ...
- linux下logrotate 配置和理解
对于Linux 的系统安全来说,日志文件是极其重要的工具.系统管理员可以使用logrotate 程序用来管理系统中的最新的事件,对于Linux 的系统安全来说,日志文件是极其重要的工具.系统管理员可以 ...
- DECO 一个REACT NAtive 开发IDE工具
DECO 一个REACT NAtive 开发IDE工具. 目前只支持 OS,NO WINDOWS https://www.decosoftware.com/ 一个方便的快速 ERXPRESS 教程:h ...
- poj 1700
http://poj.org/problem?id=1700 题目大意就是一条船,有N个人需要过河,求N个人最短过河的时间 #include <stdio.h> int main() { ...
- 如何为自己的windows 8系统的电脑更换锁屏壁纸
现在的人都喜欢个性,今天教大家如何设置自己想要的锁屏壁纸 工具/原料 Windows 8系统的笔记本电脑 方法/步骤 将鼠标移到电脑的右下方,点击设置按钮进入设置页面 找到更改电脑设置并点击进 ...
- ACM/ICPC 之 最长公共子序列计数及其回溯算法(51Nod-1006(最长公共子序列))
这道题被51Nod定为基础题(这要求有点高啊),我感觉应该可以算作一级或者二级题目,主要原因不是动态规划的状态转移方程的问题,而是需要理解最后的回溯算法. 题目大意:找到两个字符串中最长的子序列,子序 ...
- GNOME启动时激活NumLock(小键盘数字锁定)
首先下载numlockx官方源提供的安装包,解压后进入目录运行终端,切换到root账户执行以下命令: python ./setup.py 然后依次点击GNOME菜单项上的“系统->首选项-> ...
- Mathematics:Pseudoprime numbers(POJ 3641)
强伪素数 题目大意:利用费马定理找出强伪素数(就是本身是合数,但是满足费马定理的那些Carmichael Numbers) 很简单的一题,连费马小定理都不用要,不过就是要用暴力判断素数的方法先确定是 ...
- linux查看系统版本和系统位数
1. uname -a you will view kernel name.network node hostname.kernel release.kernel version.machine h ...
- 【leetcode】Reverse Linked List II (middle)
Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1-> ...