访问Facebook好友更新

时间:2011-04-27 10:56:29

标签: php facebook api cakephp

如何获得通过facebook api查看我朋友的更新的权限?开发维基并没有真正帮助我......我只需要一个代码示例......

1 个答案:

答案 0 :(得分:2)

您需要为用户提供read_stream扩展权限。 http://developers.facebook.com/docs/authentication/permissions/

如果您在活动的用户会话期间执行此操作(例如,不使用offline_access扩展权限),那么阅读更新将变得非常简单。

示例(使用php):

<?php
require 'facebook.php';

// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
    'appId'  => 'YOUR APP ID',
    'secret' => 'YOUR APP SECRET',
    'cookie' => true,
));

try
{
    $user_feed = $facebook->api('/me/home/');

    /**
     * You now have the users's news feed, you can do with it what you want.
     * If you want to prune it for friends only...you need to do a little more
     * work..
    **/
    $friend_only_feed = array();

    if (!empty($user_feed['data']))
    {
        $user_feed_pagination = $user_feed['paging'];
        $user_feed = $user_feed['data'];

        $friends = $facebook->api('/me/friends', 'GET');

        $friend_list = array();

        if (!empty($friends['data']))
        {
            $friends = $friends['data'];


            foreach ($friends as $friend)
            {
                $friend_list []= $friend['id'];
            }
        }

        $friend_only_feed = array();
        foreach ($user_feed as $story)
        {
            if (in_array($story['from']['id'], $friend_list))
            {
                $friend_only_feed []= $story;
            }
        }
    }

}
catch (FacebookApiException $e)
{
    /**
     * you don't have an active user session or required permissions 
     * for this user, so rdr to facebook to login.
    **/

    $loginUrl = $facebook->getLoginUrl(array(
        'req_perms' => 'publish_stream'
    ));

    header('Location: ' . $loginUrl);
    exit;
}


print_r($friend_only_feed);

这应该包括获取用户的新闻Feed,以及从朋友那里获取所有帖子(不包括网页更新)。如果您没有访问权限,它会将用户重定向到登录状态并为您提供访问权限。

值得注意的是,默认的home端点只能让您回到过去的25个故事。如果您需要返回更远的地方,Facebook响应中的paging键可让您执行多个请求以返回更远,或者您可以将数组传递给api()方法告诉Facebook您想要更大的限制

<?php
$user_feed = $facebook->api('/me/home/', 'GET', array(
    'limit' => 500
));