Call.php 3.16 KB
<?php
namespace Plugin\Partner\douban;

use Plugin\Partner\Factory;

define('DB_CLASS_PATH', dirname(__FILE__).'/class/');
require DB_CLASS_PATH . 'DoubanClient.class.php';


/**
 * 豆瓣的调用接口
 * 
 * @name Call
 * @package lib/partner/douban
 * @copyright yoho.inc
 * @version 5.0 (2015-12-31 10:54:54)
 * @author xiaowei <xiaowei.gong@yoho.cn>
 */
class Call extends Factory
{
    /*人人网对象*/
    protected $douban;

    /**
     * 初始化
     */
    protected function init()
    {
        $this->douban = new \DoubanClient($this->apiConfig['appId'], $this->apiConfig['appKey'], $this->apiConfig['callback'], $this->apiConfig['scope']);
    }

    /**
     * 获取授权URL
     *
     * @return string
     */
    public function getAuthorizeUrl()
    {
        return $this->douban->getAuthorizeURL();
    }

    /**
     * 获取授权的TOKEN
     *
     * @return array
     */
    public function getAccessToken()
    {
        $token = array();

        if (isset($_GET['code']))
        {
            try
            {
                // 设置authorizeCode
                $this->douban->setAuthorizeCode($_GET['code']);
                // 通过authorizeCode获取accessToken,至此完成用户授权
                $this->douban->requestAccessToken();

                $token = (array) $this->douban->getToken();
            }
            catch (Exception $e)
            {
                // do nothing
            }
        }

        return $token;
    }

    /**
     * 获取当前用户的基本资料
     *
     * @param array $token  授权成功的TOKEN, 默认为NULL
     * @return array
     */
    public function getUserInfo($token)
    {
        $userInfo = array();

        if (is_array($token) && isset($token['access_token']))
        {
            // 获得保存的token
            $this->douban->setAccessToken($token);

            // 获得当前登录用户信息
            $userInfo = $this->douban->api('/v2/user/~me', 'GET', null, true);
        }

        return $userInfo;
    }

    /**
     * 获取当前用户的偶像(关注)列表
     *
     * 此接口暂未开放
     * 
     * @return array
     */
    public function getFriends($token, $params)
    {
        return array();
    }

    /**
     * 同步分享
     *
     * @param String $image 新鲜事图片地址
     * @param String $content 新鲜事主体内容 注意:最多200个字符。
     * @param String $link 新鲜事标题和图片指向的链接
     * @return Long 发布新鲜事的ID
     */
    public function syncShare($token, $content, $image, $link)
    {
        $result = false;

        if (is_array($token) && isset($token['access_token']))
        {
            // 获得保存的token
            $this->douban->setAccessToken($token);

            // 发表一篇说说
            $param = array('source' => $this->apiConfig['appKey'], 'text' => $content, 'rec_image' => $image, 'rec_url' => $link,);
            $result = $this->douban->api('/shuo/v2/statuses', 'POST', $param, true);
        }

        return $result;
    }

}