Home > php教程 > PHP源码 > body text

豆瓣的账号登录及api操作

PHP中文网
Release: 2016-05-25 17:06:51
Original
1312 people have browsed it

        跳至         

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;
	}
}
Copy after login

2. [文件] config.php ~ 311B


3. [文件] index.php ~ 899B

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 '点击进入授权页面';
}
?>
Copy after login

4. [文件] callback.php ~ 799B

Copy after login


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template