5

i am using this curl class for saving the file -->

class CurlHelper
{
  /**
   * Downloads a file from a url and returns the temporary file path.
   * @param string $url
   * @return string The file path
   */
  public static function downloadFile($url, $options = array())
  {
    if (!is_array($options))
      $options = array();
    $options = array_merge(array(
        'connectionTimeout' => 5, // seconds
        'timeout' => 10, // seconds
        'sslVerifyPeer' => false,
        'followLocation' => false, // if true, limit recursive redirection by
        'maxRedirs' => 1, // setting value for "maxRedirs"
        ), $options);

    // create a temporary file (we are assuming that we can write to the system's temporary directory)
    $tempFileName = tempnam(sys_get_temp_dir(), '');
    $fh = fopen($tempFileName, 'w');

    $curl = curl_init($url);
    curl_setopt($curl, CURLOPT_FILE, $fh);
    curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $options['connectionTimeout']);
    curl_setopt($curl, CURLOPT_TIMEOUT, $options['timeout']);
    curl_setopt($curl, CURLOPT_HEADER, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, $options['sslVerifyPeer']);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, $options['followLocation']);
    curl_setopt($curl, CURLOPT_MAXREDIRS, $options['maxRedirs']);
    curl_exec($curl);

    curl_close($curl);
    fclose($fh);

    return $tempFileName;
  }
}

    $url = 'http://graph.facebook.com/shashankvaishnav/picture';
$sourceFilePath = CurlHelper::downloadFile($url, array(
  'followLocation' => true,
  'maxRedirs' => 5,
));

this above code will give me the temporary url in $sourceFilePath variable now i want to store that image in my image folder. I am stuck here...please help me in this...thank you in advance.

  • $tempFileName = tempnam(sys_get_temp_dir(), ''); this code save the file as xxxx.tmp, it supposed to save as .jpg as @senthilbp mentioned in his answer. – thomasbabuj Nov 02 '12 at 09:18

4 Answers4

19

There is a pretty easy option for this:

$url = 'http://graph.facebook.com/shashankvaishnav/picture';
$data = file_get_contents($url);
$fileName = 'fb_profilepic.jpg';
$file = fopen($fileName, 'w+');
fputs($file, $data);
fclose($file);

You don´t even need anything else then.

Or if file_get_contents is disabled (usually it isn´t), this should also work:

$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, 'http://graph.facebook.com/shashankvaishnav/picture');
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
curl_close($ch);
$fileName = 'fb_profilepic.jpg';
$file = fopen($fileName, 'w+');
fputs($file, $data);
fclose($file);

Edit: This is not possible anymore, since you can´t use the username for API calls anymore. You have to use an (App Scoped) ID after authorizing a User with the Facebook API instead of the username.

andyrandy
  • 72,880
  • 8
  • 113
  • 130
0
$filename = 'abcd.jpg';    
$to = 'image/'.$filename;
if(copy($sourceFilePath,$to))
echo 'copied';
else
echo 'error';
Guru
  • 644
  • 6
  • 18
0
$fbprofileimage = file_get_contents('http://graph.facebook.com/senthilbp/picture/'); 
file_put_contents('senthilbp.gif', $fbprofileimage);
senthilbp
  • 807
  • 1
  • 9
  • 16
  • this is not working giving very big error message --> HTTP/1.1 302 Found Access-Control-Allow-Origin: * Cache-Control: private, no-cache, no-store, must-revalidate Content-Type: image/jpeg Expires: Sat, 01 Jan 2000 00:00:00 GMT Location: http://profile.ak.fbcdn.net/hprofile-ak-snc6/203228_100000407223160_1936571676_s.jpg Pragma: no-cache X-FB-Rev: 661961 X-FB-Debug: +t1EQ3gRio5a6XjuG0Q+uQnUlM+M78/2EZYBXkIsyV4= Date: Fri, 02 Nov 2012 11:08:19 GMT Connection: keep-alive Content-Length: 0 – Shashank M Vaishnav Nov 02 '12 at 11:08
  • Copy this code in a PHP file , create folder named "fbimg" and give write permission to that folder , I mentioned **facebookid** -- give your facebook id and run in browser , your profiile image will be downloaded in "fbimg" folder – senthilbp Nov 02 '12 at 11:44
  • i tried the same but that above mentioned text is coming and image is not comming :( – Shashank M Vaishnav Nov 02 '12 at 12:38
  • sit this file_get_contents is disabled on our server...they are not allowing us to do it – Shashank M Vaishnav Nov 02 '12 at 13:19
0
$profile_Image = 'http://graph.facebook.com/shashankvaishnav/picture';
$userImage = $picturtmp_name . '.jpg'; // insert $userImage in db table field.
$savepath = '/images/';
insert_user_picture($savepath, $profile_Image, $userImage);

function insert_user_picture($path, $profile_Image, $userImage) {
  $thumb_image = file_get_contents($profile_Image);
  $thumb_file = $path . $userImage;
  file_put_contents($thumb_file, $thumb_image);
}
Rakesh Sharma
  • 13,680
  • 5
  • 37
  • 44
  • i think that's a most part to get api response for image all functions work on allow_url_fopen = on (copy,fputs,file_put_contents) you acn get image using curl but for saving in folder you have allow_url_fopen = on in your php.ini – Rakesh Sharma Nov 03 '12 at 03:44