Sithu
Sithu

Reputation: 4421

Facebook PHP SDK with Graph API

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

Upvotes: 1

Views: 2615

Answers (2)

Sithu
Sithu

Reputation: 4421

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

Upvotes: 0

Aslam Patel
Aslam Patel

Reputation: 894

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();

Upvotes: 2

Related Questions