Call.class.php 7.01 KB
<?php
define('FB_CLASS_PATH', dirname(__FILE__).'/class/');
require FB_CLASS_PATH . 'Facebook.class.php';

/**
 * Facebook的调用接口
 * 
 * @name Lib_Partner_Facebook_Call
 * @package lib/partner/facebook
 * @copyright yoho.inc
 * @version 4.0 (2013-12-19 13:34:09)
 * @author fei.hong <fei.hong@yoho.cn>
 */
class Lib_Partner_Facebook_Call extends Lib_Partner_Factory
{
    /*客户端操作对象*/
    protected $client;
    
    /*控制是否使用国外代理*/
    protected $useProxy = true;
    
    /**
     * 初始化
     */
    protected function init() 
    {
        $this->client = new Facebook(array('appId' => $this->apiConfig['appId'], 'secret' => $this->apiConfig['appKey']));
    }
    
    /**
     * 获取授权URL
     * 
     * @return string
     */
    public function getAuthorizeUrl()
    {
        return $this->client->getLoginUrl(array('redirect_uri' => $this->apiConfig['callback']));
    }
    
    /**
     * 获取授权的TOKEN
     * 
     * @return array
     */
    public function getAccessToken()
    {
        $token = array();
    
        if (isset($_REQUEST['code']))
        {
            try
            {
                $token['access_token'] = $this->client->getAccessToken();
                $token['expires_in'] = 1393566713;
                if ($token['access_token'])
                {
                    $token['openid'] = $this->client->getUser();
                }
            }
            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']))
        {
            $fields = 'id,name,username,email,picture,gender';
            try
            {
                if ($this->useProxy)
                {
                    $params['accessToken'] = $token['access_token'];
                    $params['fields'] = $fields;
                    
                    $userInfo = $this->api('me', $params);
                }
                else
                {
                    $this->client->setAccessToken($token['access_token']);
        
                    $params = array('fields' => $fields,);
                    
                    $userInfo = $this->client->api('/me', $params);
                }
            }
            catch (FacebookApiException $e)
            {
                // do nothing
                // echo $e->getMessage();
            }
        }
    
        return $userInfo;
    }
    
    /**
     * 获取当前用户的基本资料
     *
     * @param array $token  授权成功的TOKEN, 默认为NULL
     * @return array
     */
    public function getFriends($token, $params)
    {
        $friends = array();
    
        if (is_array($token) && isset($token['access_token'], $token['openid']))
        {
            try
            {
                if ($this->useProxy)
                {
                    $params['accessToken'] = $token['access_token'];
                    
                    $name = $token['openid'] . '/friends';
                    
                    $friends = $this->api($name, $params);
                }
                else
                {
                    $api = '/' . $token['openid'] . '/friends';
                    
                    $this->client->setAccessToken($token['access_token']);
                    
                    $friends = $this->client->api($api, $params);
                }
            }
            catch (FacebookApiException $e)
            {
                // do nothing
                // echo $e->getMessage();
            }
        }
        
        return $friends;
    }
    
    /**
     * 同步分享
     *
     * @param array $token  授权成功的TOKEN
     * @param string $content  要更新的微博信息。信息内容不超过140个汉字, 为空返回400错误。
     * @param string $image  要发布的图片路径, 支持url。[只支持png/jpg/gif三种格式, 增加格式请修改get_image_mime方法]
     * @param string $link  URL地址, 通过该地址链接回来
     * @return boolean  false:失败, true:成功
     */
    public function syncShare($token, $content, $image, $link)
    {
        $result = false;
    
        if (is_array($token) && isset($token['access_token']))
        {
            try
            {
                if ($this->useProxy)
                {
                    // allow: 'EVERYONE', 'ALL_FRIENDS', 'FRIENDS_OF_FRIENDS', 'CUSTOM', 'SELF'
                    $params = array();
                    $params['accessToken'] = $token['access_token'];
                    $params['body'] = array('message' => $content, 'picture' => $image, 'link' => $link, 
                                            'name' => '', 'caption' => '', 'description' => '', 'allow' => 'EVERYONE');
                    
                    $name = $token['openid'] . '/feed';
                    
                    $result = $this->api($name, $params, 'POST');
                }
                else
                {
                    $params = array('method' => 'POST', 'message' => $content, 'picture' => $image, 'link' => $link, );
                    
                    $api = '/' . $token['openid'] . '/feed';
                    
                    $this->client->setAccessToken($token['access_token']);
                    
                    $result = $this->client->api($api, $params);
                }
            }
            catch (FacebookApiException $e)
            {
                // do nothing
                // echo $e->getMessage();
            }
        }
    
        return $result;
    }
    
    /**
     * 私有的调用接口方法
     * 
     * 备注: 因国内被禁不能直接访问FB, 所以通过此方法委托国外的服务器向FB请求,并返回结果.
     * 
     * @param string $name  接口名称
     * @param array $params  请求的参数
     * @param string $method  请求方式 GET/POST
     */
    private function api($name, $params = array(), $method = 'GET')
    {
        $url = 'http://198.11.182.44/fb';
        
        $hander = curl_init();
        $options = array(
            CURLOPT_HEADER => 0,
            CURLOPT_TIMEOUT => 30,
            CURLOPT_RETURNTRANSFER => 1,
        );
        
        $params['method'] = $name;
        
        if ($method === 'GET')
        {
            $options[CURLOPT_URL] = $url . '?data=' . json_encode($params);
        }
        else
        {
            $params = array('data' => json_encode($params),);
            
            $options[CURLOPT_URL] = $url;
            $options[CURLOPT_POSTFIELDS] = $params;
        }
        
        curl_setopt_array($hander, $options);
        $response = curl_exec($hander);
        curl_close($hander);
        
        $result = $response ? json_decode($response, true) : array();
        
        return $result;
    }
}