5
votes

Hi I'm using facebook php sdk to make posts to my fanpage. I am attempting to schedule these posts to the future. I am running into some problems though. Here is my code

<?php
// This code is just a snippet of the example.php script
// from the PHP-SDK <https://github.com/facebook/facebook-php-sdk/blob/master/examples/example.php>
require_once('facebookphp/src/facebook.php');

$app_id = "xxxxx";
$app_secret = "xxxxxx";

// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
  'appId'  => $app_id,
  'secret' => $app_secret,
  'fileUpload' => true,
));

// Get User ID
$user = $facebook->getUser();
var_dump($user); 
if ($user) {
  try {
    $page_id = 'xxxx';
    $album_id = 'xxxxx';
    $page_info = $facebook->api("/$page_id?fields=access_token");
    if( !empty($page_info['access_token']) ) {
        $args = array(
            'access_token'  => $page_info['access_token'],
            'scheduled_publish_time' => "1361642425", #an example timestamp
            'message'       => "test post",
            'source'        => "@" . "/path/to/photo.jpg",
            'published' => "0",

        );
        $post_id = $facebook->api("/$album_id/photos","post",$args);
        #echo $post_id;
    } else {
        $permissions = $facebook->api("/me/permissions");
        if( !array_key_exists('publish_stream', $permissions['data'][0]) ||
            !array_key_exists('manage_pages', $permissions['data'][0])) {
            // We don't have one of the permissions
            // Alert the admin or ask for the permission!
            header( "Location: " . $facebook->getLoginUrl(array("scope" => "publish_stream, manage_pages")) );
        }

    }
  } catch (FacebookApiException $e) {
    var_dump($e);
    $user = null;
  }
}

// Login or logout url will be needed depending on current user state.
if ($user) {
  $logoutUrl = $facebook->getLogoutUrl();
  echo '<a href="'.$logoutUrl.'">logout</a>';
} else {
  $loginUrl = $facebook->getLoginUrl(array('scope'=>'manage_pages,publish_stream'));
  echo '<a href="'.$loginUrl.'">login</a>';
}

// ... rest of your code
?>

This code posts a photo to my facebook page scheduled into the future perfectly, except when the schedule time comes to pass the photo is not published. In the activity log the photo remains in the 'scheduled posts' section with the error 'Sorry, something went wrong publishing this scheduled post'

I suspected this was because of the parameter: 'published' => "0",

If I remove this parameter or set it to 1 then the post is not made at all and I get the error 'You cannot specify a scheduled publish time on a published post'

1
Turns out if I use $page_id instead of $album_id in $post_id = $facebook->api("/$album_id/photos","post",$args); then it sort of works. I still get an error saying that it couldn't publish, then about an hour later it seems to publish anyway.... facebook, you are weird....Lucas Scholten
Same problem here, except that publishing in an hour is not an option. We got a tight schedule in our case.jayarjo
Scheduled posts only work with scheduling times larger than 10mins. So what the timeframe you usually are trying to publish the photos upfront?Tobi
Hi. I have the same problem. Did you resolve it ? Do you have an app in dev mode to ?mcbjam
@mcbjam Please see my first comment. "Turns out if I use $page_id instead of $album_id in $post_id = $facebook->api("/$album_id/photos","post",$args); then it sort of works. I still get an error saying that it couldn't publish, then about an hour later it seems to publish anyway"Lucas Scholten

1 Answers

3
votes

Scheduling the post with the above code, works perfect work me. I just tried and scheduled the post with 11 minutes later and I got the notification after 11 minutes and photo was published on the said album.

Actually its some kinda facebook bug.

Just go to each post, click on 'Reschedule' and adjust the time by a 15 minutes (or however much you want). For some reason this resets them individually and everything goes back to normal, the posts will once again post themselves according to schedule.

I know this is a tedious way to fix something which Facebook should fix on their own, but it works.