Skip to content

Instantly share code, notes, and snippets.

@akostadinov

akostadinov/fb2ical.php

Forked from phibo23/fb2ical.php
Last active Aug 29, 2015
Embed
What would you like to do?
<?php
/*
original code by cogitom: https://gist.github.com/997980/
phibo23: https://gist.github.com/2808256/
This script reads future events (plus several days into the past) from Facebook pages
and creates a subscribable iCalendar
Improvements upon the original version:
- avoid setting DTEND field if facebook event lacks an end time
- fixed start and endtime reading
- timezone adjustment done on facebook's side (more reliable)
- restrict to public events
- more detailed location (include venue or link to facebook page, depending on what is available)
- ignore exeptions (e.g. no events)
How to use:
- go to developers.facebook.com
- create an app
- paste app id and secret into the $config array below
- set pageId to the desired page id to get events from or use the fbpage GET parameter
- upload file along with facebook sdk and iCalcreator to your site
- set the require PATHs of the above libs properly after 'require_once'
- now opening http://you.site.example.com/path/fb2ical.php should return ical data
*/
// https://github.com/facebook/facebook-php-sdk
require_once 'path/to/facebook.php';
// http://www.kigkonsult.se/iCalcreator/
require_once 'path/to/iCalcreator.class.php';
$config = array(
'appId' => 'xxxx',//change to your fb app id
'secret' => 'yyyy',//change to your fb app secret
'pageId' => $_GET['fbpage'],//get the pageid from passed parameter 'fbpage'
'timezone' => 'Europe/Berlin',
'timezoneDif' => 9*60*60,
);
$facebook = new Facebook(array('appId'=>$config['appId'], 'secret'=>$config['secret']));
$page = $facebook->api('/'.$config['pageId']);
$v = new vcalendar(array('unique_id'=>$config['pageId']));
$v->setProperty('method', 'PUBLISH' );
$v->setProperty('x-wr-calname', $page['name']);
$v->setProperty('X-WR-CALDESC', $page['name']);
$v->setProperty('X-WR-TIMEZONE', $config['timezone']);
try {
$cons = $facebook->api('/'.$config['pageId'].'/events');
$event_ids = array();
//the times given here - unlike the ones retrieved from an event object - are already adjusted to your local time.
$start_times = array();
$end_times = array();
foreach ($cons['data'] as $con) {
$event_ids[] = $con['id'];
$start_times += array($con['id'] => preg_replace('/[-T:]/', '', $con['start_time']));
$end_times += array($con['id'] => preg_replace('/[-T:]/', '', $con['end_time']));
}
$events = $facebook->api('?date_format=U&ids='.implode(',', $event_ids));
foreach ($events as $event){
//only add public events
if ($event['privacy'] == "OPEN"){
$etz = $config['timezone'];
$e = & $v->newComponent('vevent');
$e->setProperty('dtzid',$etz);
$e->setProperty('dtstart', $start_times[$event['id']]);
if ($end_times[$event['id']]) {
$e->setProperty('dtend', $end_times[$event['id']]);
}
$location = $event['location'];
// include street address or link to facebook page in location
if ($event['venue']){
$venues = $event['venue'];
$venue="";
if (!$venues['id']){
foreach ($venues as $vn) {
$venue = $venue.", ".$vn;
}
} else {
$getid = $facebook->api('/'.$venues['id']);
$venue = " (".$getid['link'].")";
}
$location = $location." @ ".$venue;
}
$e->setProperty('location', $location);
$e->setProperty('summary', $event['name']);
$e->setProperty('description', $event['description']);
$e->setProperty('url', 'http://www.facebook.com/event.php?eid='.$event['id']);
}
}
} catch (Exception $e) {
//ignore Exeptions (e.g. if there are no events)
}
$v->returnCalendar();
@akostadinov

This comment has been minimized.

Copy link
Owner Author

@akostadinov akostadinov commented Mar 17, 2015

FYI if event lacks time (has only date set) then this script does not set proper event start time and client app may not read event properly. Can be fixed perhaps in the loop of line #56.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.