PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, February 27, 2022

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

 February 27, 2022     facebook-php-sdk, guzzle, php     No comments   

Issue

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)?


Solution

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.



Answered By - SamHecquet
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing