1

I spent 2 days to search the any available resources for tutorial/example/doc for Facebook PHP SDK with Graph API for fetching Group Feed and Page Feed. My question can be a duplicate/similar to a post here.

I have gone through the Facebook PHP SDK Doc. I may be overlooked or searching at the wrong place. Basically, what I want to understand is:

  1. How to retrieve group/page feed from Graph API using PHP SDK
  2. How to retrieve post details (including photos) from group/page

I am using facebook-php-sdk-v4-5.0.0

1

2 Answers 2

2

Use PHP SDK v5

Use for Group feed this code

$request = new FacebookRequest(
  $session,
  'GET',
  '/{group-id}/feed'
);
$response = $request->execute();
$graphObject = $response->getGraphObject();

For page feed use this code

$request = new FacebookRequest(
  $session,
  'GET',
  '/{page-id}/feed'
);
$response = $request->execute();
$graphObject = $response->getGraphObject();
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your code, but this is very general. Can you be more specific for how you get that $session variable?
0

I finally reached to the answer of my own questions somehow. Thank you all for paying your attention and answering. But please allow me to post my own answer as I am finally able to figure it out and hope it could be a help for those who is new to Graph API like me.

I figure it out by reading the MarkDowns from downloaded Facebook API docs folder as below:

C:\facebook-php-sdk-v4-5.0.0\docs

C:\facebook-php-sdk-v4-5.0.0\docs

Following is the code snippet for my requirement:

<?php
require_once( 'Facebook/autoload.php' );

use Facebook\Facebook;
use Facebook\FacebookApp;
use Facebook\FacebookRequest;

$app_id = "appid";
$secret = "secret";
$access_token = "accesstoken";

$fb = new Facebook([
  'app_id' => $app_id,
  'app_secret' => $secret,
  'default_graph_version' => 'v2.5',
  'default_access_token' => $access_token,
  ]);

// Create request
// $fbApp = new FacebookApp($app_id, $secret);
// $request = new FacebookRequest($fbApp, $access_token, 'GET', '/GROUPID/posts');

// Alternative request
// $request = $fb->request('GET', '/GROUPID/posts');


// Send the request to Graph
try {
    // $response = $fb->getClient()->sendRequest($request);
    $response = $fb->get('/GROUPID/posts');
} catch(Facebook\Exceptions\FacebookResponseException $e) {
  // When Graph returns an error
  echo 'Graph returned an error: ' . $e->getMessage();
  exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
  // When validation fails or other local issues
  echo 'Facebook SDK returned an error: ' . $e->getMessage();
  exit;
}

echo "<pre>";
// print_r($response->getDecodedBody());

$decodedBody = $response->getDecodedBody();
$data = $decodedBody['data'];

foreach ($data as $key => $value) {
    try {
        $request = $fb->request('GET', '/'.$value['id'].'?fields=id,message,picture,object_id,attachments');
        $post = $fb->getClient()->sendRequest($request);

    } catch(Facebook\Exceptions\FacebookResponseException $e) {
      // When Graph returns an error
      echo 'Graph returned an error: ' . $e->getMessage();
      exit;
    } catch(Facebook\Exceptions\FacebookSDKException $e) {
      // When validation fails or other local issues
      echo 'Facebook SDK returned an error: ' . $e->getMessage();
      exit;
    }
    print_r($post);
}

Graph API Explorer Result

Graph API Explorer Result

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.