The following code is inside a file called facebook_posts.php which I call from my index file like so: <?php require_once("http://www.example.com/design/php/facebook_posts.php"); ?>
. However, where this code is put, there is no response. So neither success, nor the catch errors return an error (as I see it). I tried absolute URLs, but that didn't work either. (I hid the api and page information.) Apparently the content that follows the require_once (footer and scripts) aren't loaded. Something seems to go wrong when including the SDK.
I'm not using composer, do I need to require
the Facebook\
files or use
them? And which ones do I need for retrieving posts from a page?
<?php
// Defining FB SDK with absolute paths
define('FACEBOOK_SDK_V4_SRC_DIR', 'http://example.com/design/Facebook/');
require 'http://example.com/design/php/autoload.php';
use Facebook\FacebookSession;
use Facebook\FacebookRequest;
use Facebook\GraphUser;
use Facebook\FacebookRequestException;
FacebookSession::setDefaultApplication('{my-app-id}','{my-app-secret}');
$session = new FacebookSession('{my-long-lived-access-token}');
// Get the GraphUser object for the current user:
try {
$request = new FacebookRequest(
$session,
'GET',
'/{my-page-id}/feed'
);
$response = $request->execute();
$graphObject = $response->getGraphObject();
var_dump($graphObject);
echo graphObject;
echo "banana";
} catch (FacebookRequestException $e) {
echo "API ERROR";
} catch (\Exception $e) {
echo "other error";
}
?>
EDIT: so I just required in all the FB files, and that seems to work. However, I don't know how to traverse/iterate the object that is returned. I.e. how to loop through the different posts (the four latest posts by the page) and echo them in HTML. A base structure would look like this:
<time>{publish date}</time>
<p>{post message}</p>
<a href="{link to included url}">{title to included url}</a>
You need to use a long-lived page access-token.
Page Access Token
These access tokens are similar to user access tokens, except that they provide permission to APIs that read, write or modify the data belonging to a Facebook Page. To obtain a page access token you need to start by obtaining a user access token and asking for the manage_pages permission. Once you have the user access token you then get the page access token via the Graph API.
As @CBroe said, you should not use that access token in client-side code as it is secret/private and you don't want anyone to get it.
So for what you want to do, Javascript is not the right choice. You will have to use some server-side code, like PHP, Python or Ruby to get the posts. If that is clear, here is how you can create it.
Create a Facebook app:
The application has disabled OAuth client flow
.You need to create a user access token.
manage_pages
in the "Extended Permissions" tab.Get your short-lived page access token.
me/accounts
(GET
),Get your long-lived page access token.
https://graph.facebook.com/oauth/access_token?client_id=(1)&client_secret=(2)&grant_type=fb_exchange_token&fb_exchange_token=(3)
in the address bar,access_token=FAKECAALBygJ4juoBAJyb8Cbq9bvwPYQwIaX53fLTMAWZCmDan1netI30khjITZAqcw9uE0lRT4ayWGm2ZCS7s7aZCVhF3ei6m0fuy2AkTkwmzUiJI4NUOZAyZAzuL
,Now you can use that new access token to retrieve the posts of your page:
$session = new FacebookSession('FAKECAALBygJ4juoBAJyb8Cbq9bvwPYQwIaX53fLTMAWZCmDan1netI30khjITZAqcw9uE0lRT4ayWGm2ZCS7s7aZCVhF3ei6m0fuy2AkTkwmzUiJI4NUOZAyZAzuL');
try {
$data = (new FacebookRequest(
$session, 'GET', '/me/posts'
))->execute()->getGraphObject()->getPropertyAsArray("data");
foreach ($data as $post){
$postId = $post->getProperty('id');
$postMessage = $post->getProperty('message');
print "$postId - $postMessage <br />";
}
} catch (FacebookRequestException $e) {
// The Graph API returned an error
} catch (\Exception $e) {
// Some other error occurred
}