Reputation: 4421
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:
I am using facebook-php-sdk-v4-5.0.0
Upvotes: 1
Views: 2615
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
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
Upvotes: 0
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