Heim > php教程 > PHP源码 > Hauptteil

豆瓣的账号登录及api操作

PHP中文网
Freigeben: 2016-05-25 17:06:51
Original
1286 Leute haben es durchsucht

        跳至         

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;
	}
}
Nach dem Login kopieren

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 '点击进入授权页面';
}
?>
Nach dem Login kopieren

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

Nach dem Login kopieren


Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Empfehlungen
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!