I'm trying to code a script that'll post to a Facebook page. So to that end, I was able to cobble together a script that relies on Facebook's 5.x SDK, which I installed via composer. You'll notice in the below code that it doesn't have app id, app secret, nor page id. In the actual script, it does have the needed values, and it's already activated via Facebook dev center. When I execute the script, it simply fails without throwing up an error, in spite of added code to catch exceptions. I've searched Google and PHPBuilder to see if others ran into the same issue, found a few posts on it on Stack Overflow, but those posters also ran into dead-ends. So I'm hoping one of you have an eagle eye and can see where I'm going wrong. This is actually my first time working with Facebook's SDK.
<?
error_reporting(E_ALL);
ini_set('display_errors', '1');
// Pass session data over.
session_start();
// Include the required dependencies.
require_once('../../../vendor/autoload.php');
echo 'Dependencies loaded.<br>';
// Initialize the Facebook PHP SDK v5.
$fb = new Facebook\Facebook([
'app_id' => '{app_id}',
'app_secret' => '{app_secret}',
'default_graph_version' => 'v2.3',
]);
echo 'Facebook SDK initialized.<br>';
$pageId = '{page_id}'; # DeafScoop ID
echo 'Attempting to login...<br>';
$helper = $fb->getRedirectLoginHelper();
echo 'LoginHelper loaded, now getting Access Token...<br>';
try {
$accessToken = $helper->getAccessToken();
echo 'Done, moving on...<br>';
} catch(Facebook\Exceptions\FacebookSDKException $e) {
// There was an error communicating with Graph
echo 'There was an error.<br>';
echo $e->getMessage();
exit();
}
if (isset($accessToken)) {
echo 'Attempting to exchange for long term token...<br>';
$client = $fb->getOAuth2Client();
try {
$accessToken = $client->getLongLivedAccessToken($accessToken);
} catch(Facebook\Exceptions\FacebookSDKException $e) {
echo 'There was an error.<br>';
echo $e->getMessage();
exit;
}
echo 'Successfully logged in. Now searching for page ID...<br>';
$response = $fb->get('/me/accounts', (string) $accessToken);
foreach ($response->getDecodedBody() as $allPages) {
foreach ($allPages as $page ) {
if (isset($page['id']) && $page['id'] == $pageId) { // Suppose you save it as this variable
$appAccessToken = (string) $page['access_token'];
echo 'Found it!.<br>';
break;
}
}
}
echo 'Attempting to post...<br>';
$response = $fb->post(
'/'.$pageId.'/feed',
array(
"message" => "Message",
"link" => "http://www.example.com",
"picture" => "http://www.example.net/images/example.png",
"name" => "Title",
"caption" => "www.example.com",
"description" => "Description example"
),
$appAccessToken
);
// Success
echo 'Success! Done.<br>';
$postId = $response->getGraphNode();
echo $postId;
exit();
} elseif ($helper->getError()) {
echo 'There was a serious error. Dumping data.<br>';
var_dump($helper->getError());
var_dump($helper->getErrorCode());
var_dump($helper->getErrorReason());
var_dump($helper->getErrorDescription());
exit();
}
# Ruh roh
echo 'Should not have ended up here, something went very wrong!<br>';
#echo $e->getMessage();
var_dump($helper->getError());
var_dump($helper->getErrorCode());
var_dump($helper->getErrorReason());
var_dump($helper->getErrorDescription());
exit();
?>
This is what it returns when I run the script:
Dependencies loaded.
Facebook SDK initialized.
Attempting to login...
LoginHelper loaded, now getting Access Token...
Done, moving on...
Should not have ended up here, something went very wrong!
NULL NULL NULL NULL
Based on the debugging text I added to track the flow, it appears it fails at this point:
if (isset($accessToken)) {
Then from there it skips to the end of the script. Obviously it's due to $accessToken not being set/being null, so I'm able to pinpoint the main problem as being this line (because that's where it should have been set):
$accessToken = $helper->getAccessToken();
But what's strange is that I have code put in there to catch an exception; not once is it triggered, which implies it was successfully set.
This is where I've run into a wall and am not sure how to proceed from here. Your input would help. Thanks in advance for your time and consideration in this matter.