Call.php 6.63 KB
<?php
namespace Plugin\Partner\renren;

use Plugin\Partner\Factory;

define('RENREN_CLASS_PATH', dirname (__FILE__) . '/class/');
require RENREN_CLASS_PATH . 'RennClientBase.class.php';

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

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

    /**
     * 获取授权URL
     *
     * @return string
     */
    public function getAuthorizeUrl()
    {
        return $this->renn->getAuthorizeURL($this->apiConfig['callback'], 'code', null, null, true, $this->apiConfig['scope']);
    }

    /**
     * 获取授权的TOKEN
     *
     * @return array
     */
    public function getAccessToken()
    {
        $token = array();
        
        if (isset($_REQUEST['code'])) 
        {
            $keys = array();
            $keys['code'] = $_REQUEST['code'];
            $keys['redirect_uri'] = $this->apiConfig['callback'];
            
            try 
            {
                $token = $this->renn->getTokenFromTokenEndpoint('code', $keys, TokenType::Bearer);
                
                // 返回数组格式的信息
                $token = $this->renn->getTokens();
            } 
            catch (Exception $e) 
            {
                // do nothing
            }
        }
        
        return $token;
    }

    /**
     * 获取当前用户的基本资料
     *
     * @see http://open.renren.com/wiki/API/v2/user/get
     * @param object $token  授权成功的TOKEN, 默认为NULL
     * @return array
     */
    public function getUserInfo($token)
    {
        $userInfo = array();

        if (!empty($token))
        {
            if (is_array($token))
            {
                $token = new AccessToken(isset($token['type']) ? $token['type'] : TokenType::Bearer, $token['accessToken'], isset($token['refreshToken']) ? $token['refreshToken'] : null, isset($token['macKey']) ? $token['macKey'] : null, isset($token['macAlgorithm']) ? $token['macAlgorithm'] : null);
            }
            // 获得保存的token
            $this->renn->authWithToken($token);

            // 获得当前登录用户
            if (isset($token->accessToken))
            {
                $parts = explode('-', $token->accessToken);
                if (isset($parts[1]))
                {
                    $params = array('userId' => $parts[1]);
                    try 
                    {
                        $userInfo = $this->renn->execute('/v2/user/login/get', 'GET', $params, array(), array());
                    }
                    catch (Exception $e)
                    {
                        // do nothing
                    }
                }
            }
        }

        return $userInfo;
    }

    /**
     * 获取当前用户的偶像(关注)列表
     *
     * @see http://open.renren.com/wiki/V2/friend/list
     * @param object $token  访问令牌
     * @param array $params  参数列表
     *     Long $userId 用户ID。该字段默认为当前用户
     *     Integer $pageSize 页面大小。默认大小500。
     *     Integer $pageNumber 页码。取值大于零,默认值为1
     * @return array
     */
    public function getFriends($token, $params)
    {
        $friends = array();

        if (!empty($token))
        {
            if (is_array($token))
            {
                $token = new AccessToken(isset($token['type']) ? $token['type'] : TokenType::Bearer, $token['accessToken'], isset($token['refreshToken']) ? $token['refreshToken'] : null, isset($token['macKey']) ? $token['macKey'] : null, isset($token['macAlgorithm']) ? $token['macAlgorithm'] : null);
            }
            // 获得保存的token
            $this->renn->authWithToken($token);

            // 获取当前登录用户的好友列表
            if (!isset($params['userId']) && isset($token->accessToken))
            {
                $parts = explode('-', $token->accessToken);
                if (isset($parts[1]))
                {
                    $params['userId'] = $parts[1];
                }
            }
            
            try
            {
                $friends = $this->renn->execute('/v2/user/friend/list', 'GET', $params, array(), array());
            }
            catch (Exception $e)
            {
                // do nothing
            }
        }

        return $friends;
    }

    /**
     * 同步分享
     *
     * 发送自定义新鲜事。新鲜事会发布用户的个人动态信息到用户人人网主页,同时会出现在好友的新鲜事中
     * 
     * @see http://open.renren.com/wiki/API/v2/feed/put 
     * @param object $token  访问令牌
     * @param String $image  新鲜事图片地址
     * @param String $content  新鲜事主体内容 注意:最多200个字符。
     * @param String $link  新鲜事标题和图片指向的链接
     * @return Long 发布新鲜事的ID
     */
    public function syncShare($token, $content, $image, $link)
    {
        $result = false;

        if (!empty($token))
        {
            if (is_array($token))
            {
                $token = new AccessToken(isset($token['type']) ? $token['type'] : TokenType::Bearer, $token['accessToken'], isset($token['refreshToken']) ? $token['refreshToken'] : null, isset($token['macKey']) ? $token['macKey'] : null, isset($token['macAlgorithm']) ? $token['macAlgorithm'] : null);
            }
            // 获得保存的token
            $this->renn->authWithToken($token);
            
            $params = array('title' => '来自YOHO的分享', 'message' => $content, 'actionTargetUrl' => $link,
                            'imageUrl' => $image, 'description' => $content, 'targetUrl' => $link,);

            try
            {
                $result = $this->renn->execute('/v2/feed/put', 'POST', $params, array(), array());
            }
            catch (Exception $e)
            {
                // do nothing
            }
        }

        return $result;
    }
    
    /**
     * 返回token的所有信息(包括user)
     * 
     * 备注:所有此方法必须先调用 getAccessToken()
     * 
     * @return array
     */
    public function getTokens()
    {
        return $this->renn->getTokens();
    }
    
}