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>