Watercress's account login and API operation

Source: Internet
Author: User

1 . douban.php
<?php/** * php Library for douban.com * * @author piscdong (http://www.piscdong.com/) */class doubanphp{function __        Construct ($client _id, $client _secret, $access _token=null) {$this->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 ' and $callback _url, ' scope ' and $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 ' and $this->cli     Ent_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 ' = ' ref Resh_token ', ' refresh_token ' = = $refresh _token, ' client_id ' and '->client_id $this ', ' cl        Ient_secret ' = $this->client_secret, ' Redirect_uri ' and $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 ' and ' $url, ' Rec_desc ' and $description, ' Rec_im Age ' = $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
<?php//configuration file header (' content-type:text/html; Charset=utf-8 '); $douban _k= '; Watercress Application API key$douban_s= '; Watercress application secret$callback_url= ' http://yoururl/callback.php '; Authorization callback URL $scope= ' Douban_basic_common,shuo_basic_w '; Permissions list, see official API documentation for specific permissions?>
3
<?phpsession_start (); require_once (' config.php '); require_once (' douban.php '); $douban _t=isset ($_session[' douban_t ')? $_session[' douban_t ']: '; Check if you are signed in if ($douban _t!= ") {    $douban =new doubanphp ($douban _k, $douban _s, $douban _t);     Get login user information    $result = $douban->me ();    Var_dump ($result);     /**    //access token expires with the refresh token refreshed access token    $result = $douban->access_token_refresh ($callback _url , $_session[' Douban_r ');    Var_dump ($result);    **/     /**    //Publish share    $text = ' share content ';    $title = ' share title ';    $url = ' http://www.oschina.net/';    $result = $douban->share ($text, $title, $url);    Var_dump ($result);    **/}else{    //Generate Login link    $douban =new doubanphp ($douban _k, $douban _s);    $login _url= $douban->login_url ($callback _url, $scope);    Echo ' <a href= ', $login _url, ' > Click to enter the authorization page </a> ';}? >
4 . callback.php

<?php//authorization callback page, i.e. $callback_urlsession_start () in config file, require_once (' config.php '); require_once (' douban.php '); if (isset ($_get[' Code ')) && $_get[' code ']!= ') {    $douban =new doubanphp ($douban _k, $douban _s);    $result = $douban->access_token ($callback _url, $_get[' Code ');} if (Isset ($result [' Access_token ']) && $result [' Access_token ']!= ') {    echo ' authorization completed, please record <br/>access Token:<input size= "value=" ', $result [' Access_token '], ' "><br/>refresh token:<input size=" value = "', $result [' Refresh_token '], ' > ';     Save the login information in this example using SESSION save    $_session[' douban_t ']= $result [' Access_token '];//access token    $_session[' Douban _r ']= $result [' Refresh_token ']; Refresh token}else{    echo ' authorization failed ';} Echo ' <br/><a href= './' > Return </a> ';? >

Watercress's account login and API operation

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.