Last active
December 1, 2017 07:58
-
-
Save CircleSquaredPublishing/7666814e86fbe16eb27b to your computer and use it in GitHub Desktop.
How to Collect Public Data with the Facebook Graph API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/**************************************************************************************************************************************** | |
File Name: fb_collect.php | |
Author: Circle Squared Data Labs | |
Author URI: http://www.heatery.io | |
Description: This article will benefit aspiring developers who have a developers account and registered App on Facebook but aren't sure how to programmatically integrate the Graph API into their App. Code samples are provided at the end. Feel free to copy them now and come back to the article later if you are having troubles with implementation. This tutorial assumes you have created a Facebook developers account and registered app. If you do not you will need to do that first. | |
Version: 1.0 | |
License: Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 3.0 License, and code samples are licensed under the Apache 2.0 License. | |
License URI: license.txt | |
Tags: responsive-layout, fluid-layout, custom-background, custom-menu, custom-maps, AJAX, Facebook Graph API, Heatmap Visualizations, Google Maps, Bootstrap, Heatery, Circle Squared Data Labs, restaurants, social media analysis, spatial data analysis | |
****************************************************************************************************************************************/ | |
/********************************************************************************************** | |
1. Create a file that will be used to store the results from the call to the Facebook Graph API. | |
***********************************************************************************************/ | |
$table= basename(__FILE__ , '.php'); | |
$name= ($table . '.json'); | |
$fp= fopen($name, 'w' ); | |
/*********************************************************************** | |
2. Create and execute the API call using the PHP cURL library functions. | |
************************************************************************/ | |
$ch= curl_init(); | |
$url= 'https://graph.facebook.com/v2.4/search?q=restaurant&type=place&distance=3200¢er=26.47,-80.07&fields=location,name,likes,talking_about_count,were_here_count,description,website,cover,about,culinary_team&limit=50&access_token=5893781074754054|r-KJ2eJLHOHLHlhf-eohdOH-LHd'; | |
curl_setopt($ch, CURLOPT_URL, $url); | |
curl_setopt($ch, CURLOPT_FILE, $fp); | |
curl_exec($ch); | |
curl_close($ch); | |
/****************************************** | |
3. Collect, display and decode the results. | |
*******************************************/ | |
$results= file_get_contents($name); | |
print_r($results); | |
$fb_array= json_decode($results, true); | |
/************************* | |
4. Connect to the Database. | |
**************************/ | |
require($common_path . 'fb_conn.php'); | |
/************************************************************************************* | |
5. Insert the results into the database using a prepared statement and a foreach loop. | |
**************************************************************************************/ | |
foreach ($fb_array[data] as $i) { | |
$stmt1=$conn->prepare("INSERT INTO fb_api_results | |
(FID, | |
fb_web, | |
fb_cover, | |
fb_about, | |
fb_culinary_team, | |
fb_description, | |
fb_name, | |
fb_likes, | |
fb_were_here, | |
fb_talking_about, | |
fb_street, | |
fb_city, | |
fb_state, | |
fb_zip, | |
fb_lat, | |
fb_lng) | |
VALUES | |
(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"); | |
$stmt1->bind_param("dssssssiiisssidd", | |
$FID, | |
$fb_web, | |
$fb_cover, | |
$fb_about, | |
$fb_culinary_team, | |
$fb_description, | |
$fb_name, | |
$fb_likes, | |
$fb_were_here, | |
$fb_talking_about, | |
$fb_street, | |
$fb_city, | |
$fb_state, | |
$fb_zip, | |
$fb_lat, | |
$fb_lng); | |
$FID= mysqli_real_escape_string($conn, $i['id']); | |
$fb_web= mysqli_real_escape_string($conn, $i['website']); | |
$fb_cover= mysqli_real_escape_string($conn, $i['cover']['source']); | |
$fb_about= mysqli_real_escape_string($conn, $i['about']); | |
$fb_culinary_team= mysqli_real_escape_string($conn, $i['culinary_team']); | |
$fb_description= mysqli_real_escape_string($conn, $i['description']); | |
$fb_name= mysqli_real_escape_string($conn, $i['name']); | |
$fb_likes= mysqli_real_escape_string($conn, $i['likes']); | |
$fb_were_here= mysqli_real_escape_string($conn, $i['were_here_count']); | |
$fb_talking_about= mysqli_real_escape_string($conn, $i['talking_about_count']); | |
$fb_street= mysqli_real_escape_string($conn, $i['location']['street']); | |
$fb_city= mysqli_real_escape_string($conn, $i['location']['city']); | |
$fb_state= mysqli_real_escape_string($conn, $i['location']['state']); | |
$fb_zip= mysqli_real_escape_string($conn, $i['location']['zip']); | |
$fb_lat= mysqli_real_escape_string($conn, $i['location']['latitude']); | |
$fb_lng= mysqli_real_escape_string($conn, $i['location']['longitude']); | |
$stmt1->execute(); | |
} | |
$stmt1->close(); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment