【问题标题】:Posting to user feed > image is missing发布到用户提要 > 图像丢失
【发布时间】:2012-06-13 10:22:59
【问题描述】:
我的应用正在向用户 Facebook 提要发布。一切正常,但图片不见了。我不知道为什么。
这是 $facebook->api 方法使用的数组:
Array
(
[message] => MyText
[link] => MyLink
[name] => MyName
[picture] => https://lala.herokuapp.com/images/oceanblue.png
[caption] => MyCaption
[description] => MyDescription
)
图片可访问(不是上面的真实网址),当我在浏览器中打开网址时显示。
api 返回对象如下所示:
Array
(
[id] => 652685341_10151011701170342
)
非常感谢您的帮助,谢谢! :)
【问题讨论】:
标签:
php
image
facebook-graph-api
facebook-php-sdk
【解决方案1】:
我想做同样的事情。您无法将远程图片上传到 Facebook,您需要将图片保存在您的服务器上,因此我将图片下载到我的服务器上然后上传。
这是我的代码:
/**
* Post photo on facebook
* This function requires the user to be logged in
* This function requires the 'publish_stream' permission
*
* Fetches the picture from remote URL and uploads it to facebook
* @param string $picture Picture URL
* @param string $description Description to place in caption
* @param string $link Link to place in caption
* @param string $albumId Id of the previously created album
*/
function postPhotoOnFacebook($picture, $description, $link, $albumId){
global $userId, $facebook;
$tempFilename = $_SERVER['DOCUMENT_ROOT'].'/temp/';
$tempFilename .= uniqid().'_'.basename($picture);
if ($userId) {
try {
if($imgContent = @file_get_contents($picture)){
if(@file_put_contents($tempFilename, $imgContent)){
$photo_details = array('message' => "$link $description");
$photo_details['image'] = '@' . realpath($tempFilename);
$data = $facebook->api('/'.$albumId.'/photos', 'post', $photo_details);
unlink($tempFilename);
}
}
} catch (FacebookApiException $e) {
error_log($e);
$userId = null;
}
}
}
这个堆栈问题很有帮助:Upload a remote photo to an upload
希望有帮助!