简体   繁体   English

Facebook PHP SDK 4-获得最后5篇帖子

[英]Facebook PHP SDK 4 - get last 5 posts

I am trying to get the last five posts. 我正在尝试获取最后五个职位。 I have created the following: 我创建了以下内容:

try {
    // Returns a `Facebook\FacebookResponse` object
    $response = $this->fb->get(
    '/'.$page_id.'/posts',
    $access_token
    );

} catch(Facebook\Exceptions\FacebookResponseException $e) {
  echo 'Graph returned an error: ' . $e->getMessage();
  exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
  echo 'Facebook SDK returned an error: ' . $e->getMessage();
  exit;
}

$graphNode = $response->getGraphEdge();
echo "<pre>";
print_r($graphNode);
echo "</pre>";
exit;

How can I limit the output? 如何限制输出? For example, instead of 100 records being returned, I only want 5. I know I need to use 'limit=5' but don't know where in above to place it. 例如,我只需要5条记录,而不是返回100条记录。我知道我需要使用'limit = 5',但是不知道在上面放置它的位置。

Also, with the above script, I get a massive Facebook\\GraphNodes\\GraphEdge Object with all sorts of info. 另外,使用上述脚本,我得到了包含各种信息的大型Facebook \\ GraphNodes \\ GraphEdge Object。 No way to get a smaller more refined object just for posts (eg. title, body, picture, date)? 没有办法只为帖子(例如标题,正文,图片,日期)获取更小,更精致的对象吗?

As I told you in the comments, you need to add to the url you're calling all these parameters like the limit or the fields you want. 正如我在评论中告诉您的那样,您需要将要调用的所有这些参数(例如limit或想要的字段)添加到url中。

So it should be like 所以应该像

$response = $this->fb->get(
    '/'.$page_id.'/posts?fields=id,title,created_time&amp;limit=‌​5',
    $access_token
 );

If you want to know which fields are available, you can use the documentation but this list may not be accurate based on which API version your using. 如果您想知道哪些字段可用,可以使用文档,但是根据您使用的API版本,此列表可能并不准确。 Another way is to inspect the object before specifying the fields. 另一种方法是在指定字段之前检查对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM