[英]How to use the graph API to upload photos to albums
My code(not working): 我的代码(不起作用):
<?php
require '../src/facebook.php';
$facebook = new Facebook(array(
'appId' => '...',
'secret' => '...'
));
Facebook::$CURL_OPTS[CURLOPT_SSL_VERIFYPEER] = false;
$user = $facebook->getUser();
if ($user) {
try {
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
if ($user) {
$album_id = '246592692052269';
$FILE_PATH = 'Language.jpg';
$access_token = $_SESSION['fb_appId_access_token'];
$logoutUrl = $facebook->getLogoutUrl();
$args = array('image'=> '@' . __DIR__.$FILE_PATH,
'name' => 'this photo was taken in acre',
'message'=> 'Photo Caption');
$ch = curl_init();
$url = 'http://graph.facebook.com/'.$album_id.'/photos?access_token='.$access_token;
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);
//returns the photo id
if(empty($data)) {
echo 'Responde: '. $data . '<pre>';
print_r(curl_getinfo($ch));
echo '</pre>';
} else {
echo 'Upload failed! erro='. curl_error($ch);
}
} else {
$loginUrl = $facebook->getLoginUrl();
}
?>
Output: 输出:
Array
(
[url] => http://graph.facebook.com/246592692052269/photos?access_token=...
[content_type] =>
[http_code] => 0
[header_size] => 0
[request_size] => 0
[filetime] => -1
[ssl_verify_result] => 0
[redirect_count] => 0
[total_time] => 0.203
[namelookup_time] => 0
[connect_time] => 0.203
[pretransfer_time] => 0.203
[size_upload] => 0
[size_download] => 0
[speed_download] => 0
[speed_upload] => 0
[download_content_length] => -1
[upload_content_length] => -1
[starttransfer_time] => 0
[redirect_time] => 0
)
Can someone point out my error? 有人可以指出我的错误吗? Thanks in advance. 提前致谢。
Make sure file uploads are enabled on the server side, and initialize the SDK with fileUpload
set to true
as described on the PHP SDK documentation page, or call setFileUploadSupport
. 确保在服务器端启用了文件上传,并按照PHP SDK文档页面上的说明将fileUpload
设置为true
来初始化SDK,或调用setFileUploadSupport
。 You don't need to manually call the graph API with the access token and curl
commands as you are doing above, the PHP SDK will take care of that for you. 您不需要像上面一样手动使用访问令牌和curl
命令手动调用graph API,PHP SDK会为您解决这一问题。 See the api
method documentation page for a complete example. 有关完整示例,请参见api
方法文档页面。
查看facebook的howto: http : //developers.facebook.com/blog/post/498/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.