使用Facebook Graph API上传照片时出现异常 [英] Exception when uploading photo with Facebook Graph API

查看:112
本文介绍了使用Facebook Graph API上传照片时出现异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将照片上传到Facebook,为用户提供应用程序的默认相册。这在以下发布中有所描述:
http://developers.facebook.com / docs / reference / api / photo

I would like to upload a photo to facebook for a user in the default album for an application. This is described under publishing here: http://developers.facebook.com/docs/reference/api/photo

此方法已被回答:如何使用Facebook Graph API将照片上传到相册。我使用以下内容:

The method has been answered here: How can I upload photos to album using Facebook Graph API. I am using the following:

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

但是,当我尝试这个时,我得到异常(#324)需要上传文件。我有一个有效的会话,我有publish_stream和user_photos权限。我可以使用API​​检索数据。图像文件绝对有效,因为它可以加载 file_get_contents(realpath(image.png))

However I get the exception "(#324) Requires upload file" when I attempt this. I have a valid session and I have the publish_stream and user_photos permissions. I can retrieve data using the API. The image file is definitely valid because it can be loaded with file_get_contents(realpath("image.png")).

我已经尝试了这个解决方案,使用curl,这是完美的:
用Facebook的Graph API上传照片到专辑

I've tried this solution, using curl, which works perfectly: Upload Photo To Album with Facebook's 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);

与Facebook的PHP SDK卷曲相似(使用相同的$ args和$ url):

Compared with Facebook's PHP SDK curl which looks like this (using the same $args and $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);

为什么PHP版本不工作?看起来像http_build_query()函数干扰加载图像。我不清楚卷曲,以了解这里发生了什么。

Why doesn't the PHP version work? Looks like the http_build_query() function is interfering with loading the image. I don't know enough about curl to understand what's going on here.

推荐答案

我很高兴我走了问题
您必须将fileUpload参数设置为true!

I'm so glad I went trough the same problem You have to set the fileUpload param to true !

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

这篇关于使用Facebook Graph API上传照片时出现异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆