豆瓣 的账号登录及api操作,使用oauth 2.0
官方提供的sdk都太过庞大,这是我自己简化的,提供简单的账号登录、获取个人信息、发布分享等功能,如果需要其他功能可以根据官方的api文档自行添加
[文件] douban.php
01 | <?php |
02 | /** |
03 | * PHP Library for douban.com |
04 | * |
05 | * @author php100 (http://www.php100.com/) |
06 | */ |
07 | class doubanPHP |
08 | { |
09 | function __construct($client_id, $client_secret, $access_token=NULL){ |
10 | $this->client_id=$client_id; |
11 | $this->client_secret=$client_secret; |
12 | $this->access_token=$access_token; |
13 | } |
14 |
15 | function login_url($callback_url, $scope=''){ |
16 | $params=array( |
17 | 'response_type'=>'code', |
18 | 'client_id'=>$this->client_id, |
19 | 'redirect_uri'=>$callback_url, |
20 | 'scope'=>$scope, |
21 | 'state'=>md5(time()) |
22 | ); |
23 | return 'https://www.douban.com/service/auth2/auth?'.http_build_query($params); |
24 | } |
25 |
26 | function access_token($callback_url, $code){ |
27 | $params=array( |
28 | 'grant_type'=>'authorization_code', |
29 | 'code'=>$code, |
30 | 'client_id'=>$this->client_id, |
31 | 'client_secret'=>$this->client_secret, |
32 | 'redirect_uri'=>$callback_url |
33 | ); |
34 | $url='https://www.douban.com/service/auth2/token'; |
35 | return $this->http($url, http_build_query($params), 'POST'); |
36 | } |
37 |
38 | function access_token_refresh($callback_url, $refresh_token){ |
39 | $params=array( |
40 | 'grant_type'=>'refresh_token', |
41 | 'refresh_token'=>$refresh_token, |
42 | 'client_id'=>$this->client_id, |
43 | 'client_secret'=>$this->client_secret, |
44 | 'redirect_uri'=>$callback_url |
45 | ); |
46 | $url='https://www.douban.com/service/auth2/token'; |
47 | return $this->http($url, http_build_query($params), 'POST'); |
48 | } |
49 |
50 | function me(){ |
51 | $params=array(); |
52 | $url='https://api.douban.com/v2/user/~me'; |
53 | return $this->api($url, $params); |
54 | } |
55 |
56 | function share($text, $title, $url, $description='', $pic=''){ |
57 | $params=array( |
58 | 'text'=>$text, |
59 | 'rec_title'=>$title, |
60 | 'rec_url'=>$url, |
61 | 'rec_desc'=>$description, |
62 | 'rec_image'=>$pic |
63 | ); |
64 | $url='https://api.douban.com/shuo/v2/statuses/'; |
65 | return $this->api($url, $params, 'POST'); |
66 | } |
67 |
68 | function api($url, $params, $method='GET'){ |
69 | $headers[]="Authorization: Bearer ".$this->access_token; |
70 | if($method=='GET'){ |
71 | $result=$this->http($url.'?'.http_build_query($params), '', 'GET',$headers); |
72 | }else{ |
73 | $result=$this->http($url, http_build_query($params), 'POST', $headers); |
74 | } |
75 | return $result; |
76 | } |
77 |
78 | function http($url, $postfields='', $method='GET', $headers=array()){ |
79 | $ci=curl_init(); |
80 | curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, FALSE); |
81 | curl_setopt($ci, CURLOPT_RETURNTRANSFER, 1); |
82 | curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, 30); |
83 | curl_setopt($ci, CURLOPT_TIMEOUT, 30); |
84 | if($method=='POST'){ |
85 | curl_setopt($ci, CURLOPT_POST, TRUE); |
86 | if($postfields!='')curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields); |
87 | } |
88 | $headers[]="User-Agent: doubanPHP(piscdong.com)"; |
89 | curl_setopt($ci, CURLOPT_HTTPHEADER, $headers); |
90 | curl_setopt($ci, CURLOPT_URL, $url); |
91 | $response=curl_exec($ci); |
92 | curl_close($ci); |
93 | $json_r=array(); |
94 | if($response!='')$json_r=json_decode($response, true); |
95 | return $json_r; |
96 | } |
97 | } |
[文件] config.php
1 | <?php |
2 | //配置文件 |
3 | header('Content-Type: text/html; charset=UTF-8'); |
4 |
5 | $douban_k=''; //豆瓣应用API Key |
6 | $douban_s=''; //豆瓣应用Secret |
7 | $callback_url='http://yoururl/callback.php'; //授权回调网址 |
8 | $scope='douban_basic_common,shuo_basic_w'; //权限列表,具体权限请查看官方的api文档 |
9 | ?> |
[文件] index.php
01 | <?php |
02 | session_start(); |
03 | require_once('config.php'); |
04 | require_once('douban.php'); |
05 |
06 | $douban_t=isset($_SESSION['douban_t'])?$_SESSION['douban_t']:''; |
07 |
08 | //检查是否已登录 |
09 | if($douban_t!=''){ |
10 | $douban=new doubanPHP($douban_k, $douban_s, $douban_t); |
11 |
12 | //获取登录用户信息 |
13 | $result=$douban->me(); |
14 | var_dump($result); |
15 |
16 | /** |
17 | //access token到期后使用refresh token刷新access token |
18 | $result=$douban->access_token_refresh($callback_url, $_SESSION['douban_r']); |
19 | var_dump($result); |
20 | **/ |
21 |
22 | /** |
23 | //发布分享 |
24 | $text='分享内容'; |
25 | $title='分享标题'; |
26 | $url='http://www.oschina.net/'; |
27 | $result=$douban->share($text, $title, $url); |
28 | var_dump($result); |
29 | **/ |
30 |
31 | }else{ |
32 | //生成登录链接 |
33 | $douban=new doubanPHP($douban_k, $douban_s); |
34 | $login_url=$douban->login_url($callback_url, $scope); |
35 | echo '<a href="',$login_url,'">点击进入授权页面</a>'; |
36 | } |
37 | ?> |
[文件] callback.php
01 | <?php |
02 | //授权回调页面,即配置文件中的$callback_url |
03 | session_start(); |
04 | require_once('config.php'); |
05 | require_once('douban.php'); |
06 |
07 | if(isset($_GET['code']) && $_GET['code']!=''){ |
08 | $douban=new doubanPHP($douban_k, $douban_s); |
09 | $result=$douban->access_token($callback_url, $_GET['code']); |
10 | } |
11 | if(isset($result['access_token']) && $result['access_token']!=''){ |
12 | echo '授权完成,请记录<br/>access token:<input size="50" value="',$result['access_token'],'"><br/>refresh token:<input size="50" value="',$result['refresh_token'],'">'; |
13 |
14 | //保存登录信息,此示例中使用session保存 |
15 | $_SESSION['douban_t']=$result['access_token']; //access token |
16 | $_SESSION['douban_r']=$result['refresh_token']; //refresh token |
17 | }else{ |
18 | echo '授权失败'; |
19 | } |
20 | echo '<br/><a href="./">返回</a>'; |
21 | ?> |
时间: 2024-11-08 19:54:07