【发布时间】:2015-03-09 04:16:20
【问题描述】:
我正在尝试通过 URL 将图片发布到 Facebook,但我不断收到错误消息:
发生异常,代码:324,消息:(#324) 需要上传文件
我确实在我的服务器上升级到了 PHP 5.5。我尝试了两种方法,一种只有'source' => $img1, where $img1 = 'http://domain./net/path/to/file.jpg' 我试图将文件读入一个变量,但我可能没有使用file_get_contentsfunction .我不能将图像与 PHP 文件放在同一个位置,因为这些图片是从与页面管理员不同的登录名加载的。无论如何,这是我尝试过的两种方法。我还尝试在file_get_contents之前获取"data:image/jpeg;base64,".
我决定尝试使用file_get_contents函数,因为在另一个堆栈溢出帖子中,这家伙似乎很幸运uploading the image as a string.
<?
if($session) {
try {
$imagetopost = "data:image/jpeg;base64,".file_get_contents($img1, false, $context);
// Upload to a user's profile. The photo will be in the
// first album in the profile. You can also upload to
// a specific album by using /ALBUM_ID as the path
$response = (new FacebookRequest(
$session, 'POST', '/me/photos', array(
'source' => $img1,
//'message' => 'User provided message',
//'place' => $business_place_id
)
))->execute()->getGraphObject();
// If you're not using PHP 5.5 or later, change the file reference to:
// 'source' => '@/path/to/file.name'
echo "Posted with id: " . $response->getProperty('id');
} catch(FacebookRequestException $e) {
echo "Exception occured, code: " . $e->getCode();
echo " with message: " . $e->getMessage();
}
}
和:
<?
if($session) {
try {
$imagetopost = "data:image/jpeg;base64,".file_get_contents($img1, false, $context);
// Upload to a user's profile. The photo will be in the
// first album in the profile. You can also upload to
// a specific album by using /ALBUM_ID as the path
$response = (new FacebookRequest(
$session, 'POST', '/me/photos', array(
'source' => $imagetopost,
//'message' => 'User provided message',
//'place' => $business_place_id
)
))->execute()->getGraphObject();
// If you're not using PHP 5.5 or later, change the file reference to:
// 'source' => '@/path/to/file.name'
echo "Posted with id: " . $response->getProperty('id');
} catch(FacebookRequestException $e) {
echo "Exception occured, code: " . $e->getCode();
echo " with message: " . $e->getMessage();
}
}
【问题讨论】:
标签: php facebook file-get-contents