Douban Account logon and api operations-PHP source code

Source: Internet
Author: User
Douban's Account logon and api operations jump

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;}}

2. [File]Config. php ~ 311B


3. [File]Index. php ~ 899B
Me (); var_dump ($ result);/** // use refresh token to refresh access token $ result = $ douban-> access_token_refresh ($ callback_url, $ _ SESSION ['douban _ R']); var_dump ($ result); ** // publish and share $ text = 'share content '; $ title = 'share title'; $ url = 'http: // www.oschina.net/'{}result?#douban-> share ($ text, $ title, $ url); var_dump ($ result ); **/} else {// Generate a logon link $ douban = new doubanPHP ($ douban_k, $ douban_s); $ login_url = $ douban-> login_url ($ callback_ur L, $ scope); echo 'click to enter the authorization page ';}?>
4. [File]Callback. php ~ 799B

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.