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).
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.
After this, wait for the notification that your file is ready for download.
The Upload Form for JSON FILE
1 2 3 4 5 6 7 8 9 10 11 12 |
<?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 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
<?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); ?> |