Is there a way to get the Facebook profile pictures of our friends and store them in our database for further use. (Like url of their facebook profiles, as if they change their profile pictures we automatically get their new profile picture) Something similar can be done with the help of graph.facebook.com and facebook api, but I'm not sure how to do that. Please can I have the code or source or even just little bit of guidance how to do that. I am using php and mysql and using xampp as my local server.
You can use the Facebook API to get the picture:
/* make the API call */
$response = $facebook->api(
"/me/picture"
);
/* handle the result */
https://developers.facebook.com/docs/graph-api/reference/user/picture/
Assuming you have the user's username, you can get the profile picture from this URL: https://graph.facebook.com/{username}/picture
For example, if you wanted my picture, you could grab the image from https://graph.facebook.com/hawkw/picture
You could use file_get_contents
in PHP to retrieve the image.
If you want to get a specific size image, you can use the url https://graph.facebook.com/{username}/picture?type={size}
, replacing {size} with square
, small
, normal
, or large
, depending on what size you want. Alternatively, you can specify specific image sizes with https://graph.facebook.com/{username}/picture?width={width}&height={height}
.
First, you're gonna want to make a table to store your images. I'll let you figure that out yourself based on the DB schema you're using. The images should be stored as BLOB
s.
Then you'll want to connect to the DB. Here's some sample code for opening a DB connection:
// Make the connect to MySQL or die
// and display an error.
$link = mysql_connect($host, $username, $password);
if (!$link) {
die('Could not connect: ' . mysql_error());
}
// Select your database
mysql_select_db ($database);
You should then insert the images into the table using a query that looks something like this:
$query = "INSERT INTO images_table ";
$query .= "(image) VALUES ('$data')";
And then call
$succeeded = mysql_query($query, $img);
Note that I'm assigning the results of mysql_query
to a variable called $succeeded
because mysql_query
returns a boolean that you can use to determine if the INSERT
command succeeded or not - it should be TRUE
if the image was successfully inserted. This will allow you to respond to errors in the DB insert process.
Please note that I've never actually used PHP (and I don't really ever plan on it) - this is all the result of a few Google searches. I haven't tested any of this code, so it may not work right out of the box, but it should be enough to get you going in the right direction.