IT Intern Blog

IT Challenges Solved

Get Facebook Events from a Business Page

Posted by IT Intern on October 2, 2009

While working on our new company website, one of my projects was to find a way to get our Facebook events to show up on our website Events page. We have a business page on Facebook, and I soon found out it’s not exactly simple to just get events from a business page. As far as I can tell, you can’t even get an RSS feed of events from a business page. The only feature of a business page that I could find that was even remotely helpful is that each tab on a business page has its own static URL.

So I came up with a php script that will grab the source code of our Facebook events tab and parse the HTML display table of upcoming events. The following is a working example that links to Victoria’s Secret’s Facebook events tab. Just save the code as a .php file on your website, and take a look (requires PHP and CURL).

<?php
/*
facebookevents.php
by The IT Intern

This page will grab the HTML table of upcoming events displayed on a Facebook business page's events tab.
It includes some CSS to format the table using Facebook's id names
*/

//Static URL of a business page's Events tab
$url = 'http://www.facebook.com/victoriassecret?v=app_2344061033&#39;; //Victoria's Secret

//Open curl, set options, get source code, close curl
$curl = curl_init($url);

curl_setopt ($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt ($curl, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt ($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);

$source = curl_exec($curl);
curl_close($curl);

//Strip html before and after events table
$begevents = strpos($source,'<div id="future_events');
$endevents = (strpos($source,'<div id="events_show_past_link'))-$begevents;

$eventscode=substr($source,$begevents,$endevents);

//Edit relative links to be absolute
$absurl = 'http://www.facebook.com&#39;;

//Search links
$start = 0;
while (strpos($eventscode,'/search/',$start) != FALSE) {
$start = strpos($eventscode,'/search/',$start);
$eventscode = substr_replace($eventscode,$absurl,$start,0);
$start += 30;
}

//Event links
$start = 0;
while (strpos($eventscode,'/event.php',$start) != FALSE) {
$start = strpos($eventscode,'/event.php',$start);
$eventscode = substr_replace($eventscode,$absurl,$start,0);
$start += 30;
}

//Add CSS
$displaycode .= '<html><head>
<title>Victoria\'s Secret\'s Events – by The IT Intern</title>
<style type="text/css">
<!--
#future_events { font-size: 14px; }
.timeline {
font-style: italic;
border-bottom: 1px solid;
}
td.tunaimage { padding: 1em; }
img { border: 0; }
td.eventtitle { padding: 1em; }
table.infotable { font-size: 12px; }
-->
</style>
</head>';

// Add events table
$displaycode .= $eventscode;

// Add closing tags
$displaycode .= '</body></html>';

echo $displaycode;

?>

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: