Saving Facebook users profile picture to a folder

I’m trying to save the profile pictures of my users into my server, but it only gives me a 0 byte jpg file whenever i execute my code.

here is the first code that i tried but didn’t work.

$fp = fopen ('fb_profilepic.jpg', 'w+');
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, 'http://graph.facebook.com/875644792482183/picture');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // follow the redirection
curl_setopt($ch, CURLOPT_FILE, $fp); // write curl response to file
curl_exec($ch); // get curl response
curl_close($ch);
fclose($fp);

this is the second code that i got from another thread

$fp = fopen ('fb_profilepic.jpg', 'w+');
$ch = curl_init('http://graph.facebook.com/875644792482183/picture');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$json = '';
if( ($json = curl_exec($ch) ) === false)
{
    echo 'Curl error: ' . curl_error($ch);
}
else
{
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // follow the redirection
    curl_setopt($ch, CURLOPT_FILE, $fp); // write curl response to file
    curl_exec($ch); // get curl response
    curl_close($ch);
    fclose($fp);

    echo 'Operation completed without any errors';
}

both of them gives me a 0 byte jpg file.enter image description here


Source: facebook

Leave a Reply