简体   繁体   中英

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.

Also, with the above script, I get a massive Facebook\\GraphNodes\\GraphEdge Object with all sorts of info. 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.

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. Another way is to inspect the object before specifying the fields.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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