I want to get the posts from a Facebook page and show them on a Wordpress site.
Firstly, I tried with the JavaScript SDK and succeeded, but couldn't figure out how to call the access token from server-side.
Secondly, I've been trying with the PHP SDK, but can not get it to work no matter what I try.
I've been over the documentation so many times I've lost count. It just doesn't make sense to me.
This is the PHP where I'm currently at:
require_once( 'out-fb/src/Facebook/autoload.php' );
$app_id = "{app-id}";
$secret = "{app-secret}";
$access_token = "{access-token}";
$fb = new Facebook([
'app_id' => $app_id,
'app_secret' => $secret,
'default_graph_version' => 'v5.0',
'default_access_token' => $access_token
]);
try {
// $response = $fb->getClient()->sendRequest($request);
$response = $fb->get('/{page-id}}/posts?fields=message,full_picture,created_time&limit=5');
} 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;
}
I don't exactly know how to translate the JSON and get the code to my front-end, but even now I get an error from WP saying "The site is experiencing technical difficulties."
If someone could guide me through the PHP SDK or maybe show me how to place the access_token server-side with the JS SDK, I'd be so damn grateful!
Thanks in advance.
So I figured it out.
I still don't know exactly how I did it, but you can see here what eventually worked for me.
I think what broke it was using $fb = new Facebook([
instead of $fb = new Facebook\Facebook([
<?php
require_once 'out-fb/src/Facebook/autoload.php';
$app_id = "{app-id}";
$app_secret = "{app-secret}";
$access_token = "{access_token}";
$fb = new Facebook\Facebook([
'app_id' => $app_id,
'app_secret' => $app_secret,
'default_graph_version' => 'v5.0',
]);
$response = $fb->get('/{page-id}/posts?fields=message,full_picture,created_time&limit=5', $access_token);
$node = $response->getGraphEdge();
echo $node->getField('3')[message];