PHP Parse Facebook Friends

PHP Parse Facebook Friends

The following tutorial will show you how to export your Facebook friends to JSON, parse this data using PHP, and import it to a MySQL database table.

Download Facebook Friends Data

To get started, you will need to login to your Facebook account and go to the “Settings” link in the menu on the right side of the page (as illustrated in the image right of this text).

 

Once you are on the settings page, you will click Your Facebook Information in the menu on the right side of the page. After that, click on the “View” link in the Download Your Information section
(as shown in the screenshot below).

Download Your Facebook Friends

 

Next, you can unselect all, since we only want Friends data. Make sure that you only select Friends.
Also, make sure to set the format to JSON, then you can click the Create File button.

Facebook Download Data

After this, wait for the notification that your file is ready for download.

[php]

<?php

echo “<form action=’uploadFacebook.php’ method=’post’ enctype=’multipart/form-data’>
Select file to upload:
<br><br>
<input type=’file’ name=’fileToUpload’ id=’fileToUpload’><br><br>
<input type=’submit’ value=’Upload File’ name=’submit’>
</form>”;

?>

[/php]

PHP Code to Upload  JSON File

The following PHP code will upload a file as long as the file does not exist in the target directory and as long as the file size is within the specified parameters.  Then, once the file is uploaded to the server the JSON data will be parsed in a foreach() loop and inserted into a MySQL database table.  The final piece of code, unlink(), deletes the .json file at the end of the script.

[php]

<?php

$target_dir = “somedirectory/uploads/”;
$target_file = $target_dir . basename($_FILES[“fileToUpload”][“name”]);
$uploadOk = 1;

// Check if file already exists
if (file_exists($target_file)) {
echo “Sorry, file already exists.”;
$uploadOk = 0;
}

// Check file size
if ($_FILES[“fileToUpload”][“size”] > 500000) {
echo “Sorry, your file is too large.”;
$uploadOk = 0;
}

// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo “Sorry, your file was not uploaded.”;
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES[“fileToUpload”][“tmp_name”], $target_file)) {
echo “The file “. basename( $_FILES[“fileToUpload”][“name”]). ” has been uploaded.”;
} else {
echo “Sorry, there was an error uploading your file.”;
}
}

include(“dbconfig.php”);

// Read JSON file
$json = file_get_contents($target_file);

//Decode JSON
$json_data = json_decode($json,true);

foreach ($json_data[‘friends’] as $friend)
{ $theName = $friend[‘name’];
$theTimestamp = $friend[‘timestamp’];

$theDate = date(“Y-m-d”, $theTimestamp);

$name_parts = explode(‘ ‘, $theName);
$firstname = $name_parts[0];
$lastname = $name_parts[sizeof($name_parts)-1];

 

$insertcount = $dbh->exec(“INSERT INTO friends(fullname, first_name, last_name, dateadded)
VALUES (‘$uid’, ‘$theName’, ‘$firstname’, ‘$lastname’, ‘$theDate’)”);

}

unlink($target_file);

?>

[/php]

Leave a Reply

Your email address will not be published. Required fields are marked *