Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
399 views
in Technique[技术] by (71.8m points)

php - Upload a remote photo to an upload

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

Instead of using

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

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

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

http://myserver.com/image.jpg

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"
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...