Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@Flower7C3
Created March 18, 2019 09:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Flower7C3/31b14ead9951ded6e685952eeb36ded0 to your computer and use it in GitHub Desktop.
Save Flower7C3/31b14ead9951ded6e685952eeb36ded0 to your computer and use it in GitHub Desktop.
Facebook calendar parser (add wordwrap after 75 characters in line)
<?php
error_reporting(0);
ini_set('display_errors', false);
# VALIDATION
if (!isset($_GET['uid'], $_GET['key'], $_GET['type'])) {
http_response_code(400);
die('No request params!');
}
if (!preg_match("'^([0-9]+)$'", $_GET['uid'])) {
http_response_code(400);
die('Invalid "user ID" request parameter!');
}
if (!preg_match("'^([A-Za-z0-9-]+)$'", $_GET['key'])) {
http_response_code(400);
die('Invalid "key" request parameter!');
}
if (!preg_match("'^(upcoming|birthdays)$'", $_GET['type'])) {
http_response_code(400);
die('Invalid "type" request parameter!');
}
# RESPONSE
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
# DOWNLOAD CALENDAR FROM FACEBOOK
$urlPattern = 'https://www.facebook.com/events/ical/%s/?uid=%s&key=%s';
$url = sprintf($urlPattern, $_GET['type'], $_GET['uid'], $_GET['key']);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
curl_close($ch);
if (!preg_match("'^BEGIN:VCALENDAR'", $response)) {
http_response_code(400);
die(sprintf('Invalid Facebook content response: %s', $response));
}
# FORMAT LINES
header('Content-type: text/calendar; charset=utf-8');
$endline = "\r\n";
$response = str_replace('X-ORIGINAL-URL:/events/', 'X-ORIGINAL-URL:https://facebook.com/events/', $response);
$response = str_replace('X-PUBLISHED-TTL:PT12H', 'X-PUBLISHED-TTL:P1H', $response);
$response = str_replace('BEGIN:VEVENT', $endline . 'BEGIN:VEVENT', $response);
$response = str_replace('END:VCALENDAR', $endline . 'END:VCALENDAR', $response);
$response = str_replace("\r", '', $response);
$lines = explode("\n", $response);
foreach ($lines as $line) {
if (preg_match("'^(ORGANIZER|SUMMARY)'", $line)) {
$line = str_replace('"', '', $line);
}
echo wordwrap($line, 75, $endline . " ", true) . $endline;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment