Mat
Mat

Reputation: 6324

How can I upload the Facebook image in my "img" folder?

I'm using the Facebook php SDK to register my users with their Facebook account. I stored all the data I need in my sql database and I'm able to access the picture with:

<img src="https://graph.facebook.com/<?php echo $user;?>/picture">

I would like to store the image (the actual image not just the link) in one of my server folder so the user can change it without accessing his Facebook profile.

Is it possible to do it?

Upvotes: 0

Views: 281

Answers (1)

h2ooooooo
h2ooooooo

Reputation: 39542

This should do what you want and put the image into images/facebook/[USERID].jpg.

Keep in mind that you should probably use cURL instead of file_get_contents so you can prepare for timeouts and long response times (which there WILL be with the facebook API.

<?php
    $imageData = file_get_contents("https://graph.facebook.com/" . $user . "/picture");
    file_put_contents("images/facebook/" . $user . ".jpg", $imageData);
?>

This is how you'd do it with cURL (untested, but should work):

<?php
    $facebookCurl = curl_init();

    curl_setopt($facebookCurl, CURLOPT_URL, "https://graph.facebook.com/" . $user . "/picture");
    curl_setopt($facebookCurl, CURLOPT_HEADER, 0);
    curl_setopt($facebookCurl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($facebookCurl, CURLOPT_TIMEOUT, 5);
    $imageData = curl_exec($facebookCurl);
    curl_close($facebookCurl);

    file_put_contents("images/facebook/" . $user . ".jpg", $imageData);
?>

Upvotes: 3

Related Questions