【问题标题】:Post Image from URL to Facebook PHP SDK using file_get_contents gets "(#324) Requires upload file"使用 file_get_contents 将图像从 URL 发布到 Facebook PHP SDK 获取“(#324) 需要上传文件”
【发布时间】: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


    【解决方案1】:

    您需要使用附加参数fileUpload = true 来初始化 facebook 客户端

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-09-16
        • 1970-01-01
        • 2017-06-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多