使用 Facebook Graph API 上传照片时出现异常
我想在应用程序的默认相册中将用户的照片上传到 Facebook。此处的发布对此进行了描述: http://developers.facebook.com/docs/reference/api/photo
该方法已在这里得到解答:如何我使用 Facebook Graph API 将照片上传到相册。我正在使用以下内容:
$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"))
加载。
我已经尝试过这个解决方案,使用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);
与 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);
为什么 PHP 版本不起作用?看起来 http_build_query() 函数正在干扰加载图像。我对curl了解不够,无法理解这里发生的事情。
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
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);
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"))
.
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);
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);
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我很高兴我遇到了同样的问题
您必须将 fileUpload 参数设置为 true !
I'm so glad I went trough the same problem
You have to set the fileUpload param to true !
Facebook 有意使用
http_build_query()
将 POST 字段转换为 GET 字符串,以阻止以 @ 开头的字段被意外或恶意地用于上传文件。 这是 GitHub 问题。对此问题的快速解决方法是删除SDK 中 src/facebook.php 中的
http_build_query()
:变为:
但是,如果您这样做,您应该采取措施过滤以 @ 开头的用户生成的消息。例如,您可以在每条消息的前面添加一个空格。
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. Here's the GitHub issue.A quick fix for this is to remove the
http_build_query()
from src/facebook.php in the SDK:Becomes:
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.
如果您使用 graph api 上传照片,则会收到错误 (#200) 用户必须已接受 TOS。
但是,如果您使用旧的 REST API,只需将 url 更改为 https://api .facebook.com/method/photos.upload?access_token=xXXXXXXXXXXX 如果您使用上面的示例。
If you use graph api to upload the photo it will getting the error (#200) User must have accepted 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.