本文共 4979 字,大约阅读时间需要 16 分钟。
豆瓣 的账号登录及api操作,使用oauth 2.0
官方提供的sdk都太过庞大,这是我自己简化的,提供简单的账号登录、获取个人信息、发布分享等功能,如果需要其他功能可以根据官方的api文档自行添加
[文件] douban.php
03 | * PHP Library for douban.com |
05 | * @author php100 (http://www.php100.com/) |
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; |
15 | function login_url($callback_url, $scope=''){ |
17 | 'response_type'=>'code', |
18 | 'client_id'=>$this->client_id, |
19 | 'redirect_uri'=>$callback_url, |
23 | return 'https://www.douban.com/service/auth2/auth?'.http_build_query($params); |
26 | function access_token($callback_url, $code){ |
28 | 'grant_type'=>'authorization_code', |
30 | 'client_id'=>$this->client_id, |
31 | 'client_secret'=>$this->client_secret, |
32 | 'redirect_uri'=>$callback_url |
34 | $url='https://www.douban.com/service/auth2/token'; |
35 | return $this->http($url, http_build_query($params), 'POST'); |
38 | function access_token_refresh($callback_url, $refresh_token){ |
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 |
46 | $url='https://www.douban.com/service/auth2/token'; |
47 | return $this->http($url, http_build_query($params), 'POST'); |
52 | $url='https://api.douban.com/v2/user/~me'; |
53 | return $this->api($url, $params); |
56 | function share($text, $title, $url, $description='', $pic=''){ |
61 | 'rec_desc'=>$description, |
64 | $url='https://api.douban.com/shuo/v2/statuses/'; |
65 | return $this->api($url, $params, 'POST'); |
68 | function api($url, $params, $method='GET'){ |
69 | $headers[]="Authorization: Bearer ".$this->access_token; |
71 | $result=$this->http($url.'?'.http_build_query($params), '', 'GET',$headers); |
73 | $result=$this->http($url, http_build_query($params), 'POST', $headers); |
78 | function http($url, $postfields='', $method='GET', $headers=array()){ |
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); |
85 | curl_setopt($ci, CURLOPT_POST, TRUE); |
86 | if($postfields!='')curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields); |
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); |
94 | if($response!='')$json_r=json_decode($response, true); |
[文件] config.php
3 | header('Content-Type: text/html; charset=UTF-8'); |
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文档 |
[文件] index.php
03 | require_once('config.php'); |
04 | require_once('douban.php'); |
06 | $douban_t=isset($_SESSION['douban_t'])?$_SESSION['douban_t']:''; |
10 | $douban=new doubanPHP($douban_k, $douban_s, $douban_t); |
13 | $result=$douban->me(); |
17 | //access token到期后使用refresh token刷新access token |
18 | $result=$douban->access_token_refresh($callback_url, $_SESSION['douban_r']); |
26 | $url='http://www.oschina.net/'; |
27 | $result=$douban->share($text, $title, $url); |
33 | $douban=new doubanPHP($douban_k, $douban_s); |
34 | $login_url=$douban->login_url($callback_url, $scope); |
35 | echo '<a href="',$login_url,'">点击进入授权页面</a>'; |
[文件] callback.php
02 | //授权回调页面,即配置文件中的$callback_url |
04 | require_once('config.php'); |
05 | require_once('douban.php'); |
07 | if(isset($_GET['code']) && $_GET['code']!=''){ |
08 | $douban=new doubanPHP($douban_k, $douban_s); |
09 | $result=$douban->access_token($callback_url, $_GET['code']); |
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'],'">'; |
14 | //保存登录信息,此示例中使用session保存 |
15 | $_SESSION['douban_t']=$result['access_token']; //access token |
16 | $_SESSION['douban_r']=$result['refresh_token']; //refresh token |
20 | echo '<br/><a href="./">返回</a>'; |
转载于:https://www.cnblogs.com/BeacherGong/archive/2013/04/03/2998260.html