PHP-通过Graph API将网络托管的照片上传到facebook相册
我正在尝试将www托管的(例如)文件上传到facebook相册 创建相册很好,但我似乎没有上传任何照片。我正在使用facebook php sdk(),我已经尝试过的示例有: 我猜CURL上传可能只能管理本地存储的文件,而不能管理web托管的文件 这是我的密码:PHP-通过Graph API将网络托管的照片上传到facebook相册,php,api,facebook,upload,photo,Php,Api,Facebook,Upload,Photo,我正在尝试将www托管的(例如)文件上传到facebook相册 创建相册很好,但我似乎没有上传任何照片。我正在使用facebook php sdk(),我已经尝试过的示例有: 我猜CURL上传可能只能管理本地存储的文件,而不能管理web托管的文件 这是我的密码: /* Try 1: $data = array(); $data['message'] = $attachment->post_title; $data['file'] = $attachment
/*
Try 1:
$data = array();
$data['message'] = $attachment->post_title;
$data['file'] = $attachment->guid;
try {
$photo = $facebook->api('/' . $album['id'] . '/photos?access_token=' . $session['access_token'], 'post', $data);
} catch (FacebookApiException $e) {
error_log($e);
}
*/
// Try 2:
//upload photo
$file = $attachment->guid;
$args = array(
'message' => 'Photo from application',
);
$args[basename($file)] = '@' . realpath(file_get_contents($file));
$ch = curl_init();
$url = 'https://graph.facebook.com/' . $album['id'] . '/photos?access_token=' . $session['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
print_r(json_decode($data,true));
…其中附件->guid包含照片url
我现在几乎被卡住了…我想问题就在这里:
$args[basename($file)] = '@' . realpath(file_get_contents($file));
因为你想发布另一个来源的图片(对吧?),所以你应该把它暂时保存在你自己的主机上
我还需要这样做,因为我必须处理图像,所以我使用了以下方法:
$im = @imagecreatefrompng('http://www.google.se/intl/en_com/images/srpr/logo1w.png');
imagejpeg($im, 'imgs/temp/temp.jpg', 85);
$args['image'] = '@' . realpath('imgs/temp/temp.jpg');
其余部分看起来不错,但我建议使用,这样会更简单,代码将与API的未来更新一起使用:
使用图形API php sdk:
$fbk = new Facebook(/* conf */);
$fbk->setFileUploadSupport(true);
//If you are executing this in a script, and not in a web page with the user logged in:
$fbk->setAccessToken(/* access token from other sources */);
//To add to an album:
$fbk->api("/$albumId/photos", "POST",
array('source' => '@'. realpath($myPhoto), 'message' => "Nice photo"));
//To upload a photo directly (the album will be created automatically):
$fbk->api("/me/photos", "POST",
array('source' => '@'. realpath($myPhoto), 'message' => "Nice photo"));
直接使用cURL:
$fbk = new Facebook(/* conf */);
$fbk->setFileUploadSupport(true);
//If you are executing this in a script, and not in a web page with the user logged in:
$fbk->setAccessToken(/* access token from other sources */);
//To add to an album:
$fbk->api("/$albumId/photos", "POST",
array('source' => '@'. realpath($myPhoto), 'message' => "Nice photo"));
//To upload a photo directly (the album will be created automatically):
$fbk->api("/me/photos", "POST",
array('source' => '@'. realpath($myPhoto), 'message' => "Nice photo"));
如果您确实想使用cURL,那么您的代码几乎是正确的,但错误在$args数组中:
$args = array(
'message' => 'Photo from application',
'source' => file_get_contents($file)
);
由于照片数据的键是source
,请参阅
请注意@in cURL:
$fbk = new Facebook(/* conf */);
$fbk->setFileUploadSupport(true);
//If you are executing this in a script, and not in a web page with the user logged in:
$fbk->setAccessToken(/* access token from other sources */);
//To add to an album:
$fbk->api("/$albumId/photos", "POST",
array('source' => '@'. realpath($myPhoto), 'message' => "Nice photo"));
//To upload a photo directly (the album will be created automatically):
$fbk->api("/me/photos", "POST",
array('source' => '@'. realpath($myPhoto), 'message' => "Nice photo"));
还请注意,cUrl中的@
意味着参数将替换为@
后面的文件的实际字节,因此如果您已经在源参数中输入了实际字节,则不需要该参数
我猜CURL上传可能只能管理本地存储的文件,而不能管理web托管的文件
不,不是这样的
但是您需要为图像提供完整的、可公开访问的HTTP URL,前面没有@
,并且必须使用参数名称URL
:
您还可以通过提供带有照片url的url
param来发布照片