Skip to content

Instantly share code, notes, and snippets.

@beweinreich
Created January 8, 2013 21:30
Show Gist options
  • Save beweinreich/4488160 to your computer and use it in GitHub Desktop.
Save beweinreich/4488160 to your computer and use it in GitHub Desktop.
A Facebook fanpage feed parser built in PHP.
<?php
// Facebook Fanpage Parser
// Author: Brian Weinreich
// Company: Rounded
// Website: http://roundedco.com
// Make sure you read through this documentation before implementing!
// Head to https://developers.facebook.com/apps and create a new app
// Log in under Rounded Developments Facebook account (Ask Brian for creds if you don't know them)
// Fill these in after creating your app
$client_id = "";
$client_secret = "";
$fanpage_id = "";
$access_token = ""; // if you have it stored in the db
// Sends get requests and returns the data
function get_data($url) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
// Grabs your apps access token
// If this fails, it will print the error and exit
function get_access_token() {
global $client_id;
global $client_secret;
$facebook_token_url = "https://graph.facebook.com/oauth/access_token?client_id=".$client_id."&client_secret=".$client_secret."&grant_type=client_credentials";
$response = get_data($facebook_token_url);
if(isset(json_decode($response)->error)) { echo $response; die; }
return $response;
}
// Grab the fanpage feed based on the access token
// If this fails, it will go and retrieve a new access token
function get_fanpage_feed($access_token) {
global $fanpage_id;
$facebook_url = "https://graph.facebook.com/".$fanpage_id."/feed?".$access_token;
$all_data = json_decode(get_data($facebook_url));
// If there is an error, it might be the access token is expired... so get a new one
if(isset($all_data->error)) {
$facebook_url = "https://graph.facebook.com/".$fanpage_id."/feed?".get_access_token();
$all_data = json_decode(get_data($facebook_url));
}
// If there is ANOTHER error, it's probably cause the facebook url is poorly formatted. we'll print it out this time and kill the app.
if(isset($all_data->error)) {
echo json_encode($all_data);
die;
}
$all_data = $all_data->data;
return $all_data;
}
// returns an array of the fanpage feed (runs through the above functions)
$feed = get_fanpage_feed($access_token);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment