php - 上传远程照片到上传

标签 php facebook facebook-graph-api

是否可以使用 Facebook PHP 库将远程图片上传到 Facebook?

而不是使用

    $facebook->setFileUploadSupport(true);
    $args = array('message' => 'My Caption');
    $args['image'] = '@' . realpath($file);

    $data = $facebook->api('/me/photos', 'post', $args);

我想使用图像的远程路径,而不是realpath($file):

http://myserver.com/image.jpg

我尝试用 http 链接替换 ​​realpath($file) 但出现以下错误:

 Uncaught CurlException: 26: couldn't open file "http://mydomain.com/fb_images/EwrTsUqEuG.jpg"

最佳答案

使用file_get_contents()将文件下载到您的服务器,然后 file_put_contents()将其存储在临时本地文件中以供上传 FB 传输过程使用,然后 unlink()之后删除该文件。

<?php

# The URL for the Image to Transfer
$imageURL = 'http://server.com/the_image.jpg';
# Folder for Temporary Files
$tempFilename = $_SERVER['DOCUMENT_ROOT'].'/tempFiles/';

# Unique Filename
$tempFilename .= uniqid().'_'.basename( $imageURL );

# Get the Image
if( $imgContent = @file_get_contents( $imageURL ) ){
  if( @file_put_contents( $tempFilename , $imgContent ) ){

    $facebook->setFileUploadSupport(true);
    $args = array('message' => 'My Caption');
    $args['image'] = '@' . realpath( $tempFilename );

    $data = $facebook->api('/me/photos', 'post', $args);

    # Once done, delete the Temporary File
    unlink( $tempFilename );

  }else{
    # Failed to Save Image
  }
}else{
  # Failed to Get Image
}

关于php - 上传远程照片到上传,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7116359/

相关文章:

javascript - 加载一组照片时,如何为每个 imageID 制作唯一的评论按钮功能?

python - 如何配置Python Twill/Mechanize库来访问Facebook

javascript - 错误 FB 未定义使用 Angular2 (CLI) 的 Facebook 登录

android - facebook android api getUsername 返回 null

php - 检查联系表是否有效(自动测试)

php - 任务(父)和任务实例(子): choose 1 image out of 6 with PHP and MySQL?

php - 了解 str_replace 和 strtr 在 php 中的工作原理

java - 使用 Fql 并尝试使用 JSON 解析结果

javascript - 如何使用 Javascript/FB.ui 打开和关闭弹出窗口

javascript - Facebook Javascript API 调用 me/invitable_friends 在 cordova 上仅返回 25 个结果,但在 Web 上则不然