上传远程照片到上传

Upload a remote photo to an upload(上传远程照片到上传)
本文介绍了上传远程照片到上传的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

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

Is it possible to upload a remote picture to Facebook using the Facebook PHP Library??

代替使用

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

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

代替 realpath($file) 我想使用图像的远程路径,例如:

Instead of a realpath($file) I would like to use a remote path to an image for example:

http://myserver.com/image.jpg

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

I tried to replace the realpath($file) with a http link but I got the following error:

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

推荐答案

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

Use file_get_contents() to download the file to your server, then file_put_contents() to store it in a temporary, local file for the upload FB transfer process, then unlink() to delete the file afterwards.

<?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
}

这篇关于上传远程照片到上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Facebook Login with Strict Mode for Redirect URIs(使用重定向 URI 的严格模式登录 Facebook)
How to retrieve post ID for a post i#39;ve just made via the Facebook API(如何检索我刚刚通过 Facebook API 发布的帖子的帖子 ID)
Facebook PHP SDK logout(Facebook PHP SDK 注销)
Facebook SDK and Graph API Comment Deleting Error(Facebook SDK 和 Graph API 评论删除错误)
PHP amp; Facebook: facebook-debug a URL using CURL and Facebook debugger(PHP amp;Facebook: facebook-debug a URL using CURL and Facebook debugger)
Can#39;t delete photo via Facebook API?(无法通过 Facebook API 删除照片?)