使用Facebook批量请求发布到许多组无法完成

时间:2014-02-01 21:12:49

标签: php facebook-php-sdk

使用Facebook批量发布到许多群组时,我遇到了一个奇怪的问题。它只是张贴在一些而不是一些(我查看了我的活动日志)。

<?php
set_time_limit(600);
require 'facebook-sdk/src/facebook.php';

$appId = 'XXXXXXXXXXXXX';
$secret = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
$user = 'me';
$message = 'this is test';
$body_message = array('message' => $message);
$batch_query = array();
$j = 0;

$facebook = new Facebook(array(
  'appId' => $appId,
  'secret' => $secret,
  'fileUpload' => false, // optional
  'allowSignedRequest' => false
));

// Set access token
$accessToken = $facebook->getAccessToken();
$facebook->setAccessToken($accessToken);

// Get User ID
$user = $facebook->getUser();
if (!$user) {
  $loginUrl = $facebook->getLoginUrl(array(
    'scope' => 'publish_stream manage_pages user_groups',
    'redirect_uri' => 'http://localhost/post2groups/poster.php'));

  //echo urldecode($loginUrl);
  header("Location: " . $loginUrl);
}

/* Get user groups */
if ($user) {
  try {
    $groups = $facebook->api('/me/groups', 'GET');
    $num_groups = count($groups['data']);
    try {
      // Iterate through my groups, and split them in array of 50 group each
      for ($i = 0; $i < $num_groups; ++$i) {
        $batch_query[$j] = array(
          'method' => 'POST',
          'relative_url' => '/'.$groups['data'][$i]['id'].'/feed',
          'body' => http_build_query($body_message) ,
          'include_headers' => 'false');

        // Make a batch request when the array has 50 group, or we reach the last block.
        if ((($i + 1) % 50 == 0) || (($i + 1) == $num_groups)) {
          $batch_result = $facebook->api('?batch='.json_encode($batch_query), 'POST');
          print_r($batch_result);
          // I use this for debugging purpose, it is output only once, while it should output twice because I have 78 group (50 + 28).
          echo "Block num: ".$i."<br /><br /><br /><br /><br /><br />";
          sleep(13);
          // Reinitialize the array and counter
          unset($batch_query);
          $batch_query = array();
          $j = - 1; // It will increment later,
        }

        ++$j;
      }
    }

    catch(FacebookApiException $o) {
      error_log($o);
    }
  }

  catch(FacebookApiException $e) {
    error_log($e);
    $user = null;
  }
}?>

每个批次调用都有50个组ID要发布到,之后脚本会睡眠13秒,它会继续发布。 我试图看到批处理的回调中包含任何错误,但没有错误。

1 个答案:

答案 0 :(得分:-2)

更改此行:

'scope' => 'publish_stream manage_pages user_groups',

对此可能:

'scope' => 'publish_stream,manage_pages,user_groups',

(请注意额外的逗号。)