• 豆瓣的账号登录及api操作


    1.douban.php
    
    
    <?php
    /**
     * PHP Library for douban.com
     *
     * @author
     */
    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'=>$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.config.php 
    <?php
    //配置文件
    header('Content-Type: text/html; charset=UTF-8');
     
    $douban_k=''; //豆瓣应用API Key
    $douban_s=''; //豆瓣应用Secret
    $callback_url='http://yoururl/callback.php'; //授权回调网址
    $scope='douban_basic_common,shuo_basic_w'; //权限列表,具体权限请查看官方的api文档
    ?>  
    3.index.php 
    <?php
    session_start();
    require_once('config.php');
    require_once('douban.php');
     
    $douban_t=isset($_SESSION['douban_t'])?$_SESSION['douban_t']:'';
     
    //检查是否已登录
    if($douban_t!=''){
        $douban=new doubanPHP($douban_k, $douban_s, $douban_t);
     
        //获取登录用户信息
        $result=$douban->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 '<a href="',$login_url,'">点击进入授权页面</a>';
    }
    ?>
    4.callback.php

    <?php
    //授权回调页面,即配置文件中的$callback_url
    session_start();
    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 '授权完成,请记录<br/>access token:<input size="50" value="',$result['access_token'],'"><br/>refresh token:<input size="50" value="',$result['refresh_token'],'">';
     
        //保存登录信息,此示例中使用session保存
        $_SESSION['douban_t']=$result['access_token']; //access token
        $_SESSION['douban_r']=$result['refresh_token']; //refresh token
    }else{
        echo '授权失败';
    }
    echo '<br/><a href="./">返回</a>';
    ?>
  • 相关阅读:
    进程的实践与练习2
    士兵队列训练问题
    大数相加
    Ignatius and the Princess II
    Parentheses Balance (括号平衡)---栈
    简单计算器
    C++全排列函数next_permutation()和prev_permutation()
    黑白图像
    9*9乘法表
    输入5 个数按从小到大的顺序输出
  • 原文地址:https://www.cnblogs.com/qhorse/p/4843962.html
Copyright © 2020-2023  润新知