Facebook Event + Google Calendar Integration

When using the Facebook Events calendar feed within Google Calendar, many events show up only as “busy” instead of their proper name. This is because Google Calendar does not handle events with “CLASS:PRIVATE” properly.

The following PHP script is something that was thrown together quickly that grabs the feed from Facebook, replaces “CLASS:PRIVATE” with “CLASS:PUBLIC” and outputs it. Just edit the script to match your personal URL, then upload it on a PHP enabled server. Point Google Calendar to this file, and you should be able to view all your events properly again.

<?php
header('Content-type: text/calendar');
$url = "YOUR_FACEBOOK_URL_HERE"; //starts with http://www.facebook.com/ical/...
$pagetext = file_get_contents($url);
$pagetext = str_replace("CLASS:PRIVATE", "CLASS:PUBLIC", $pagetext);
echo $pagetext;
?>

12 Comments

  1. [...] we love can’t read the private events, and just shows the event as “busy”. This complicated PHP script method fixes it, but it seems daunting and scary at best. There is a better way though, and that [...]

  2. KyleHase says:

    Very useful script but anyone who runs the script can see all your events, even the private ones since the URL is hard coded.

    Here’s a more secure version.

    <?php
    if (! isset($_GET['uid']) || ! isset($_GET['key']) ||
    preg_match('/[^\d]/',$_GET['uid']) || preg_match('/[^a-z0-9]/',$_GET['key'])) {
    header("HTTP/1.0 404 Not Found");
    echo "Bad uid or key";
    exit;
    }
    $uid = $_GET['uid'];
    $key = $_GET['key'];
    header('Content-type: text/calendar');
    $url = sprintf('http://www.facebook.com/ical/u.php?uid=%d&key=%s&#039;, $uid, $key);
    $pagetext = file_get_contents($url);
    $pagetext = str_replace("CLASS:PRIVATE", "CLASS:PUBLIC", $pagetext);
    echo $pagetext;

    • Tarmo says:

      @KyleHase Nice fix, but I would also replace this
      header(‘Content-type: text/calendar’);
      with this
      header(‘Content-type: text/calendar; charset=utf-8′);
      because Facebook events are in UTF-8 and without charset, some users will have incorrect characters.

  3. manus says:

    I would also replace http:// with https:// , just as an extra measure.

  4. Paul says:

    should the file be created and uploaded as a .php or .icalc or .html it is not clear?

  5. Felix says:

    I use it with wget to work around my allow_url_fopen=0 restriction. And import it with google using the webcal-protocol: webcal://domain.com/…/fbical.php This allows google to also receive the name of the calendar.

    • Felix says:

      the code:

      <?php
      function download($url) {
      return shell_exec("wget -q -O – " . escapeshellcmd($url));
      }

      if (! isset($_GET['uid']) || ! isset($_GET['key']) ||
      preg_match('/[^\d]/',$_GET['uid']) || preg_match('/[^a-z0-9A-Z_]/',$_GET['key'])) {
      header("HTTP/1.0 404 Not Found");
      echo "Bad uid or key";
      exit;
      }

      $uid = $_GET['uid'];
      $key = $_GET['key'];

      header("Content-type: text/calendar; charset=utf-8");
      $url = sprintf('http://www.facebook.com/ical/u.php?uid=%d&key=%s&#039;, $uid, $key);
      $pagetext = download($url);
      $pagetext = str_replace("CLASS:PRIVATE", "CLASS:PUBLIC", $pagetext);

      echo $pagetext;

  6. Brandon says:

    Does this code still work? The facebook calendar is now looks like webcal://www.facebook.com/ical/u.php?uid=xxx&key=xxx. I’ve copied the script above(the original post) replaced it with my link, saved as a .php file and uploaded to my server. I browse to the page and it gives me “Bad uid or key”

    Am I doing something wrong? I’ve tried replacing the “webcal” with http and https but no luck.

  7. Jeff says:

    If you copied the script from Kyle Hase, I found that my key contained characters his script did not expect, and therefore would give the “Bad uid or key” message. Simply use this line to bypass the validity checking:

    if (! isset($_GET['uid']) || ! isset($_GET['key'])) {

    or use this to keep it, but, I am unsure what characters facebook considers valid in a key.

    if (! isset($_GET['uid']) || ! isset($_GET['key']) ||
    preg_match(‘/[^\d]/’,$_GET['uid']) || preg_match(‘/[^a-zA-Z\-0-9]/’,$_GET['key'])) {

Leave a Reply