从 $facebook->api('/me/feed') 获取以前的状态

发布于 2024-12-03 02:27:26 字数 2168 浏览 0 评论 0原文

我一直在尝试访问当前用户(登录的用户)发布的先前状态。它似乎不可靠,并且返回的状态数量似乎各不相同。

我知道您可以使用 ?limit=、?since= 和 ?until=,但是当我使用 ?limit=1000 时,这意味着足够大以获取用户之前发布的一些帖子,它变得不可预测。

我想要做的是访问我加入 Facebook 后发布的前几个状态。执行此操作的最佳方法是什么?(使用“?limit=1000”似乎有点过分!)

这会设置权限等,然后获取提要:

require 'php-sdk/src/facebook.php';

$facebook = new Facebook(array(
  'appId'  => 'xxxxxxxx',
  'secret' => 'xxxxxxxxxxxxxxxxxxxx', 
  'cookie' => true
));

$user = $facebook->getUser();

// Permissions required
$par = array();
$par['scope'] = "status_update user_about_me user_status";

$feed = null;
$profile = null;
$loginUrl = null;

if ($user) {
    $logoutUrl = $facebook->getLogoutUrl();
    try {
        // Proceed knowing you have a user who is logged in and authenticated

        // Get Feed
        $feed = $facebook->api('/me/feed');
        // Get User Profile
        $profile = $facebook->api('/me');
    } catch (FacebookApiException $e) {
        error_log($e);
        $user = null;
    }   
} 
else {
    $loginUrl = $facebook->getLoginUrl($par);
}

?>

这是我用来显示提要中的状态更新的方法:

foreach ($feed['data'] as $entry)
{
    // If its a message posted by user
    $isStatusUpdate = array_key_exists('message', $entry) and $entry['from']['name'] == $profile['name'];

    if ($isStatusUpdate)
    {
        $likes = 0;
        $comment = 0;

        // Get num of likes 
        if (array_key_exists('likes', $entry))
            $likes = count($entry['likes']); 

        // Get num of comments
        if (array_key_exists('comments', $entry))
            $comments = count($entry['comments']);

        // Display message with num of likes and comments
        echo '<div class="row"><div class="span4 columns"><h2>Post '.$counter.'</h2>';
        echo '<p>Likes '.$likes.'</br>';
        echo 'Comments '.$comments.'</p></div>';
        echo '<div class="span12 columns"><h3>'.substr($entry['created_time'], 0, 10).'</h3>';
        echo '<pre class="prettyprint">'.$entry['message'].'</pre></div>';
    }
}

I've been trying to access previous statuses posted by current user (the one that logs in). It seems to be unreliable and the number of statuses returned seems to vary.

I understand that you can use ?limit=, ?since= and ?until=, however when I use ?limit=1000 thats say large enough to get some of the earlier posts made by the user it becomes unpredictable.

What I want to be able to do is access the first few statuses I've posted since I joined Facebook. What would the best way to do this? (Using '?limit=1000' seems a bit excessive!)

This sets permissions etc and then grabs the feed:

require 'php-sdk/src/facebook.php';

$facebook = new Facebook(array(
  'appId'  => 'xxxxxxxx',
  'secret' => 'xxxxxxxxxxxxxxxxxxxx', 
  'cookie' => true
));

$user = $facebook->getUser();

// Permissions required
$par = array();
$par['scope'] = "status_update user_about_me user_status";

$feed = null;
$profile = null;
$loginUrl = null;

if ($user) {
    $logoutUrl = $facebook->getLogoutUrl();
    try {
        // Proceed knowing you have a user who is logged in and authenticated

        // Get Feed
        $feed = $facebook->api('/me/feed');
        // Get User Profile
        $profile = $facebook->api('/me');
    } catch (FacebookApiException $e) {
        error_log($e);
        $user = null;
    }   
} 
else {
    $loginUrl = $facebook->getLoginUrl($par);
}

?>

This is what I'm using to display the statuses updates from the feed:

foreach ($feed['data'] as $entry)
{
    // If its a message posted by user
    $isStatusUpdate = array_key_exists('message', $entry) and $entry['from']['name'] == $profile['name'];

    if ($isStatusUpdate)
    {
        $likes = 0;
        $comment = 0;

        // Get num of likes 
        if (array_key_exists('likes', $entry))
            $likes = count($entry['likes']); 

        // Get num of comments
        if (array_key_exists('comments', $entry))
            $comments = count($entry['comments']);

        // Display message with num of likes and comments
        echo '<div class="row"><div class="span4 columns"><h2>Post '.$counter.'</h2>';
        echo '<p>Likes '.$likes.'</br>';
        echo 'Comments '.$comments.'</p></div>';
        echo '<div class="span12 columns"><h3>'.substr($entry['created_time'], 0, 10).'</h3>';
        echo '<pre class="prettyprint">'.$entry['message'].'</pre></div>';
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

习惯成性 2024-12-10 02:27:26

你不能做 limit=1000 ,我对你得到意想不到的结果并不感到惊讶。一次可以请求的数量是有上限的,即使没有上限,您也很可能会超时并且只收到部分数据。

然而,您可以做的是发送一个使用“since”字段的请求,该字段是永远过去的,因此肯定是在用户成为 Facebook 会员之前。例如,如果您使用自 2000 年 1 月 1 日起的日期,您将获得用户转发的第一个帖子的帖子列表。

希望有帮助

You can not do limit=1000, and I'm not surprised you are getting unexpected results. There is an upper limit to how many you can request at one time, even if there was not you would more then likely get a timeout and only receive partial data.

What you could do however is send a request that uses a 'since' field that is forever ago in the past so that is certainly before the user became a facebook member. For instance if you were to use a since date of 1/1/2000 you would get a list of posts from the first one the user made forward.

Hope that helps

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文