豆瓣的账号登录及api操作
.douban.php
<?php
/**
* PHP Library for douban.com
*
* @author
*/
class doubanPHP
{
function __construct($client_id, $client_secret, $access_token=NULL){
$this->client_id=$client_id;
$this->client_secret=$client_secret;
$this->access_token=$access_token;
} function login_url($callback_url, $scope=''){
$params=array(
'response_type'=>'code',
'client_id'=>$this->client_id,
'redirect_uri'=>$callback_url,
'scope'=>$scope,
'state'=>md5(time())
);
return 'https://www.douban.com/service/auth2/auth?'.http_build_query($params);
} function access_token($callback_url, $code){
$params=array(
'grant_type'=>'authorization_code',
'code'=>$code,
'client_id'=>$this->client_id,
'client_secret'=>$this->client_secret,
'redirect_uri'=>$callback_url
);
$url='https://www.douban.com/service/auth2/token';
return $this->http($url, http_build_query($params), 'POST');
} function access_token_refresh($callback_url, $refresh_token){
$params=array(
'grant_type'=>'refresh_token',
'refresh_token'=>$refresh_token,
'client_id'=>$this->client_id,
'client_secret'=>$this->client_secret,
'redirect_uri'=>$callback_url
);
$url='https://www.douban.com/service/auth2/token';
return $this->http($url, http_build_query($params), 'POST');
} function me(){
$params=array();
$url='https://api.douban.com/v2/user/~me';
return $this->api($url, $params);
} function share($text, $title, $url, $description='', $pic=''){
$params=array(
'text'=>$text,
'rec_title'=>$title,
'rec_url'=>$url,
'rec_desc'=>$description,
'rec_image'=>$pic
);
$url='https://api.douban.com/shuo/v2/statuses/';
return $this->api($url, $params, 'POST');
} function api($url, $params, $method='GET'){
$headers[]="Authorization: Bearer ".$this->access_token;
if($method=='GET'){
$result=$this->http($url.'?'.http_build_query($params), '', 'GET', $headers);
}else{
$result=$this->http($url, http_build_query($params), 'POST', $headers);
}
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: doubanPHP(piscdong.com)";
curl_setopt($ci, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ci, CURLOPT_URL, $url);
$response=curl_exec($ci);
curl_close($ci);
$json_r=array();
if($response!='')$json_r=json_decode($response, true);
return $json_r;
}
}
.config.php
<?php
//配置文件
header('Content-Type: text/html; charset=UTF-8'); $douban_k=''; //豆瓣应用API Key
$douban_s=''; //豆瓣应用Secret
$callback_url='http://yoururl/callback.php'; //授权回调网址
$scope='douban_basic_common,shuo_basic_w'; //权限列表,具体权限请查看官方的api文档
?>
.index.php
<?php
session_start();
require_once('config.php');
require_once('douban.php'); $douban_t=isset($_SESSION['douban_t'])?$_SESSION['douban_t']:''; //检查是否已登录
if($douban_t!=''){
$douban=new doubanPHP($douban_k, $douban_s, $douban_t); //获取登录用户信息
$result=$douban->me();
var_dump($result); /**
//access token到期后使用refresh token刷新access token
$result=$douban->access_token_refresh($callback_url, $_SESSION['douban_r']);
var_dump($result);
**/ /**
//发布分享
$text='分享内容';
$title='分享标题';
$url='http://www.oschina.net/';
$result=$douban->share($text, $title, $url);
var_dump($result);
**/ }else{
//生成登录链接
$douban=new doubanPHP($douban_k, $douban_s);
$login_url=$douban->login_url($callback_url, $scope);
echo '<a href="',$login_url,'">点击进入授权页面</a>';
}
?>
.callback.php
<?php
//授权回调页面,即配置文件中的$callback_url
session_start();
require_once('config.php');
require_once('douban.php'); if(isset($_GET['code']) && $_GET['code']!=''){
$douban=new doubanPHP($douban_k, $douban_s);
$result=$douban->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'],'"><br/>refresh token:<input size="50" value="',$result['refresh_token'],'">'; //保存登录信息,此示例中使用session保存
$_SESSION['douban_t']=$result['access_token']; //access token
$_SESSION['douban_r']=$result['refresh_token']; //refresh token
}else{
echo '授权失败';
}
echo '<br/><a href="./">返回</a>';
?>
豆瓣的账号登录及api操作的更多相关文章
- 人人网的账号登录及api操作
.renren.php <?php /** * PHP Library for renren.com * * @author */ class renrenPHP { function __co ...
- 腾讯微博的账号登录及api操作
.tqq.php <?php /** * PHP Library for t.qq.com * * @author */ class tqqPHP { function __construct( ...
- QQ的账号登录及api操作
.qq.php <?php /** * PHP Library for qq.com * * @author */ class qqPHP { function __construct($app ...
- 新浪微博的账号登录及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 _ ...
- QQ,新浪,SNS等公众平台的登录及api操作
QQ的写法地址:http://www.oschina.net/code/snippet_930167_19888 Sina的写法地址:http://www.oschina.net/code/snipp ...
- git多账号登录问题
作者:白狼 出处:http://www.manks.top/git-multiply-accounts.html 本文版权归作者,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文 ...
- 通过组策略实现Firefox自动以当前域账号登录MOSS站点---(原创)
忘忧草原创,转发请保留本人的大名,谢谢,如果需要文档的请找我索取 前言 通过组策略实现基于AD的windows验证的sharepoint站点在火狐下自动以当前域账号登录. 操作步骤-在服务器添加策略工 ...
- SourceTree 03 - 跳过账号登录直接进入主界面
SourceTree系列第1篇 SourceTree 01 - git 客户端介绍(http://www.cnblogs.com/geaosu/p/8807666.html) SourceTree系列 ...
随机推荐
- Install OE and BitBake
LeapFrog Explorers: Install OE and BitBake - eLinux.org http://elinux.org/LeapFrog_Explorers:_In ...
- Dnsmasq安装与配置-搭建本地DNS服务器 更干净更快无广告DNS解析
默认的情况下,我们平时上网用的本地DNS服务器都是使用电信或者联通的,但是这样也导致了不少的问题,首当其冲的就是上网时经常莫名地弹出广告,或者莫名的流量被消耗掉导致网速变慢.其次是部分网站域名不能正常 ...
- Effective C++ -----条款44:将与参数无关的代码抽离templates
Templates生成多个classes和多个函数,所以任何template代码都不该与某个造成膨胀的template参数产生相依关系. 因非类型模板参数(non-type template para ...
- 将项目导入eclipse中出现的jsp页面报错解决
- ssm控制输出sql(二)
望时高科联通log4j # DEBUG,INFO,WARN,ERROR,FATAL LOG_LEVEL=DEBUG ---------这里对应sql的级别 log4j.rootLogger=${LOG ...
- [Android进阶]学习AccessibilityService实现微信抢红包插件
在你的手机更多设置或者高级设置中,我们会发现有个无障碍的功能,很多人不知道这个功能具体是干嘛的,其实这个功能是为了增强用户界面以帮助残障人士,或者可能暂时无法与设备充分交互的人们 它的具体实现是通过A ...
- 【leetcode】Largest Number ★
Given a list of non negative integers, arrange them such that they form the largest number. For exam ...
- 【python】入门学习(二)
键盘读取字符串: name = input('What is your first name?').strip()print("Hello " + name.capitalize( ...
- php 与 js 正则匹配
php : <?php $str='<p>xxx</p>abc';$matches = array();if(preg_match('/<p>.*<\/ ...
- supersr--addSubview和 insertSubView 区别
A addSubview B 是将B直接覆盖在A的最上层 例子: [self.view addSubview:scrollView]; A insertSubView B AtIndex:2 是将 ...