Upload a remote photo to an upload

前端 未结 1 939
庸人自扰
庸人自扰 2020-12-17 06:56

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

Instead of using

    $facebook->setFileUploadSupport(true)         


        
相关标签:
1条回答
  • 2020-12-17 07:44

    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
    }
    
    0 讨论(0)
提交回复
热议问题