IT Intern Blog

IT Challenges Solved

Archive for the ‘PHP’ Category

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).

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 = ';; //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);

$source = curl_exec($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;


//Edit relative links to be absolute
$absurl = ';;

//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; }

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

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

echo $displaycode;



Posted in Facebook, PHP, Scripts | Leave a Comment »