使用PHP SDK批量请求和附加文件

时间:2012-10-11 16:10:55

标签: php facebook sdk batch-file

我正在尝试批量请求在不同的页面上发布一张独特的照片。

为此,我希望使用批量发布请求来优化过程。

我的代码:

    $facebook = new Facebook(array('appId' => myappId, secret => mysecret, 'cookie' => true, 'fileUpload' => true, 'domain' => $_SERVER['SERVER_NAME']));

    $request[0] = array( 
        'relative_url' => 'facebookPageId1/photos'
        'method' => 'post' 
        'body' => 'access_token=page_access_token_1&message=my_message&attached_files=' . basename($picture));

    $request[1] = array(
      'relative_url' =>'facebookPageId2/photos'
      'method' => 'post'
      'body' => 'access_token=page_access_token_2&message=my_message&attached_files=' . basename($picture));

    $file[basename($picture)] = '@' . realpath($picture);
    $batch = json_encode(array_values(requests));
    $params = array('batch' => $batch);
    $params = array_merge($params, $file);

    $facebook->api('/', 'POST', $params)

现在,当我运行此代码时,我得到了以下两个请求的输出:

'{"error":{"message":"(#324) Requires upload file","type":"OAuthException","code":324}}'

那么问题是什么?

我在我的Facebook对象上将 fileUpload 设置为true,并尝试使用经典请求在网址“ pageId / photos ”上发布照片,并且效果非常好。但女巫批量请求,我总是有同样的错误。

感谢您的帮助。

编辑:好我搞错了,我的要求是错的:

$request[0] = array( 
    'relative_url' => 'facebookPageId1/photos',
    'method' => 'post',
    'body' => 'access_token=page_access_token_1&message=my_message',
    'attached_files' => basename($picture)
);

$request[1] = array(
  'relative_url' =>'facebookPageId2/photos',
  'method' => 'post',
  'body' => 'access_token=page_access_token_2&message=my_message',
  'attached_files' => basename($picture)
);

但是现在我收到了以下错误:

{"error":{"message":"File picturename.jpg has not been attached","type":"GraphBatchException"}}

1 个答案:

答案 0 :(得分:0)

这就是我的所作所为:

$files['access_token']=$access_token;
$request[0] = array( 
    'relative_url' => 'facebookPageId1/photos',
    'method' => 'POST',
    'body' => 'message=my_message',
    'attached_files' => 'file_0'
);
$files['file_0']= basename($picture);

$batchresult = $facebook->api("/?batch=".urlencode(json_encode($request)), 'POST', $files);

Facebook Batch要求你把文件放在不同的数组中,你也可以把访问令牌放在那里,你需要把它放一次。