Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CircleSquaredPublishing/c910db1bdf62f036a7f436d25a490580 to your computer and use it in GitHub Desktop.
Save CircleSquaredPublishing/c910db1bdf62f036a7f436d25a490580 to your computer and use it in GitHub Desktop.
Code snippet from a tutorial on willconkwright.com about how to gather public data using the Facebook Graph API and PHP.
<?php
/**
* @category Tutorials
* @version 1.0 [June 14, 2017]
* @author Will Conkwright
* @copyright Copyright (c) 2017 Circle Squared Publishing, LLC
* A custom function for getting public data from the Facebook Graph API
*/
/**
* Storing sensitive records outside of the webroot adds a level of
* security to your application. In the fb_token.php file we've
* defined a constant named FB_API_KEY. This constant stores the value
* of our access token.
*/
require_once ('/Users/admin/Documents/accessTokens/fb_token.php');
/**
* The facebook data array varialbe is an associative array that contains
* all the parameters to find the data we need.
*/
$facebook_data_array = array(
'base_url' => 'https://graph.facebook.com/v2.9/',
'node' => 'search?',
'query_param' => 'q=',
'root_node' => 'type=place',
'coords' => 'center=35.7796,-78.6382',
'fields' => 'fields=name,talking_about_count',
'access_token' => 'access_token=' . FB_API_KEY,
);
/**
* The get_facebook_data function accepts one argument, the array of
* values that comprise the URL to the data we are trying to collect. We
* simply concatenate the values and store the string in the $url
* variable. We're using PHP's built in curl library to pass the URL to
* the browser and storing the response in the $response varialbe.
*/
function get_facebook_data( $args ) {
/* Concatenate the array values. */
$url = $args['base_url'] . $args['node'] . $args['query_param'] . '&' . $args['root_node'] . '&' . $args['coords'] . '&' . $args['fields'] . '&' . $args['access_token'];
/* Initiate request. Store the results in the $response varialbe */
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $url );
$response = curl_exec( $ch );
curl_close( $ch );
/* Return the values in the $response variable. */
return $response;
}
/**
* Call the function and display the results in the browser.
*/
get_facebook_data( $facebook_data_array );
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment