简体   繁体   English

使用Facebook Graph API上传照片时出现例外情况

[英]Exception when uploading photo with Facebook Graph API

I would like to upload a photo to facebook for a user in the default album for an application. 我想在应用程序的默认相册中为用户上传照片到Facebook。 This is described under publishing here: http://developers.facebook.com/docs/reference/api/photo 这是在这里发布的描述: http//developers.facebook.com/docs/reference/api/photo

The method has been answered here: How can I upload photos to album using Facebook Graph API . 这里已经回答了这个方法: 如何使用Facebook Graph API将照片上传到相册 I am using the following: 我使用以下内容:

$args = array(
  'message' => 'Photo Caption', 
  'image' => '@'.realpath("image.png")
);
$data = $facebook->api('/me/photos', 'post', $args);

However I get the exception "(#324) Requires upload file" when I attempt this. 但是当我尝试这个时,我得到了异常“(#324)需要上传文件”。 I have a valid session and I have the publish_stream and user_photos permissions. 我有一个有效的会话,我有publish_stream和user_photos权限。 I can retrieve data using the API. 我可以使用API​​检索数据。 The image file is definitely valid because it can be loaded with file_get_contents(realpath("image.png")) . 图像文件绝对有效,因为它可以使用file_get_contents(realpath("image.png"))加载。

I've tried this solution, using curl, which works perfectly: Upload Photo To Album with Facebook's Graph API 我尝试过这个解决方案,使用curl,效果很好: 使用Facebook的Graph API将照片上传到相册

$args = array(
  'message' => 'Photo from application',
  'pic.png' => '@'.realpath('pic.png')
);
$tok = $session['access_token']
$url = 'http://graph.facebook.com/'.$album_id.'/photos?access_token='.$tok;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
$data = curl_exec($ch);

Compared with Facebook's PHP SDK curl which looks like this (using the same $args and $url): 与Facebook的PHP SDK curl相比,它看起来像这样(使用相同的$ args和$ url):

$ch = curl_init();
$opts = self::$CURL_OPTS;
$opts[CURLOPT_POSTFIELDS] = http_build_query($args, null, '&');
$opts[CURLOPT_URL] = $url;
curl_setopt_array($ch, $opts);
$data= curl_exec($ch);

Why doesn't the PHP version work? 为什么PHP版本不起作用? Looks like the http_build_query() function is interfering with loading the image. 看起来http_build_query()函数干扰加载图像。 I don't know enough about curl to understand what's going on here. 我不太了解curl以了解这里发生了什么。

I'm so glad I went trough the same problem You have to set the fileUpload param to true ! 我很高兴我遇到了同样的问题你必须将fileUpload param设置为true!

$facebook = new Facebook(array(
            'appId'  => $facebookapi_id,
            'secret' => $facebookapi_secret,
            'fileUpload' => true,
            'cookie' => true
          ));  

Facebook have intentionally transformed the POST fields to a GET string using http_build_query() to stop fields beginning with @ being used to accidentally or maliciously to upload files. Facebook有意将POST字段转换为GET字符串,使用http_build_query()来停止以@开头的字段,用于意外或恶意上传文件。 Here's the GitHub issue. 这是GitHub问题。

A quick fix for this is to remove the http_build_query() from src/facebook.php in the SDK: 快速解决此问题是从SDK中的src / facebook.php中删除http_build_query()

$opts[CURLOPT_POSTFIELDS] = http_build_query($params, null, '&');

Becomes: 变为:

$opts[CURLOPT_POSTFIELDS] = $params;

However if you do this you should take action to filter user generated messages that start with @. 但是,如果这样做,您应该采取措施来过滤以@开头的用户生成的消息。 For example you could add a space to the front of each message. 例如,您可以在每条消息的前面添加一个空格。

If you use graph api to upload the photo it will getting the error (#200) User must have accepted TOS. 如果您使用图形api上传照片,它将收到错误(#200)用户必须已接受TOS。

However if you use old rest api, just changing the url to https://api.facebook.com/method/photos.upload?access_token=xXXXXXXXXXXX if you use above example. 但是,如果您使用旧的rest api,只需将URL更改为https://api.facebook.com/method/photos.upload?access_token=xXXXXXXXXXXX(如果您使用上面的示例)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM