Can’t figure out why this is running in a constant forward loop between my site and facebook, never terminating on the final step (which is to post status to wall).
require 'facebook.php';
//Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
'appId' => 'XXXXXXXXXXX',
'secret' => 'XXXXXXXXXXXXXXXXXXXXX',
));
$user = $facebook->getUser();
We may or may not have this data based on whether the user is logged in.
If we have a $user id here, it means we know the user is logged into
Facebook, but we don’t know if the access token is valid. An access
token is invalid if the user logged out of Facebook.
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
if ($user) {
$logoutUrl = $facebook->getLogoutUrl();
} else {
$loginUrl = $facebook->getLoginUrl();
}
So this is a pretty dirty mash up of two different solutions I’ve found. And it actually works no problem if I do the permissions on a separate doc and then load up this next piece without the if statement. So I know I’m connected to facebook correctly….
if ($user):
if(array_key_exists('publish_stream', $permissions['data'][0]) ) {
// Permission is granted!
// Do the related task
$post_id = $facebook->api('/me/feed', 'post', array('message'=>'Testing testing 123!'));
echo "<br><br><br><h1> POSTED STATUS TO WALL!</h1>";
} else {
// We don't have the permission
// Alert the user or ask for the permission!
echo "Click Below to Enter!";
header( "Location: " . $facebook->getLoginUrl(array("scope" => "publish_stream")) );
}
The issue here is it keeps running the publish stream permissions over and over again (with the header forward and times out with too many redirects), and never catches on the if statement to actually post to the wall? I just can’t see what I’m doing wrong here. Any help greatly appreciated. Thanks
The critical line you are missing is
This gets the permissions from facebook for the current user, you need it before the array_key_exists.