代码拉取完成,页面将自动刷新
<?php
/**
* 豆瓣 API client for PHP
*
* @author PiscDong (http://www.piscdong.com/)
*/
class doubanPHP
{
public $api_url='https://api.douban.com/';
public function __construct($client_id, $client_secret, $access_token=NULL){
$this->client_id=$client_id;
$this->client_secret=$client_secret;
$this->access_token=$access_token;
}
//生成授权网址
public 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);
}
//获取access token
public 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');
}
//使用refresh token获取新的access token
public 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');
}
//获取登录用户信息
public function me(){
$params=array();
return $this->api('v2/user/~me', $params);
}
//发布分享
public function share($text, $title, $url, $description='', $pic=''){
$params=array(
'text'=>$text,
'rec_title'=>$title,
'rec_url'=>$url,
'rec_desc'=>$description,
'rec_image'=>$pic
);
return $this->api('shuo/v2/statuses', $params, 'POST');
}
//调用接口
/**
//示例:获取登录用户信息
$result=$douban->api('v2/user/~me', array(), 'GET');
**/
public function api($url, $params=array(), $method='GET'){
$url=$this->api_url.$url;
$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;
}
//提交请求
private 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: Douban.PHP(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;
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。