使用file_get_contents将图像从URL发布到Facebook PHP SDK获取“(#324)需要上传文件”

时间:2015-03-09 04:16:20

标签: php facebook file-get-contents

我试图通过网址向Facebook发布图片,但我一直收到错误:

  

发生异常,代码:324,带有消息:(#324)需要上传文件

我在服务器上升级到PHP 5.5。我尝试过两种方式,一种只有'source' => $img1,,其中$ img1 =' http://domain./net/path/to/file.jpg'我尝试将文件读入变量,但我可能没有使用file_get_contents函数。我无法将图像放在与PHP文件相同的位置,因为图片是从与页面管理员不同的登录名加载的。无论如何,这是我尝试的两种方式。我还尝试在"data:image/jpeg;base64,".

之前获取file_get_contents

我决定尝试使用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();

  }   

}

2 个答案:

答案 0 :(得分:0)

你需要使用附加参数fileUpload = true

来初始化facebook客户端

答案 1 :(得分:-1)

原来我不需要使用file_get_contents。我可以将'source'更改为'url'然后我可以将网址用于我想要使用的图片。来自Facebook的照片#pubting page:2: Use a photo that is already on the internet by publishing using the url parameter: