Last active
September 17, 2015 17:13
-
-
Save twheel/4a397f28d3532f339011 to your computer and use it in GitHub Desktop.
Facebook events importer for elefantcms
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
date_default_timezone_set('My/Time_Zone'); | |
$app_id = '1234567890'; | |
$app_secret = 'abcdefghijklmnopqrstuvwxyz'; | |
//Facebook API function to get the event details from FB | |
define('FACEBOOK_SDK_V4_SRC_DIR', $_SERVER['DOCUMENT_ROOT']."/facebook-sdk/src/Facebook/"); | |
require $_SERVER["DOCUMENT_ROOT"].'/facebook-sdk/autoload.php'; | |
use Facebook\FacebookSession; | |
use Facebook\FacebookRequest; | |
use Facebook\FacebookRequestException; // need this? | |
use Facebook\FacebookAuthorizationException; // need this? | |
FacebookSession::setDefaultApplication($app_id, $app_secret); | |
$token = $app_id . '|' . $app_secret; | |
$session = new FacebookSession($token); | |
$businesses = businesses\Business::query () | |
->where ("facebook <> ''") | |
->fetch (); | |
// Make array of venue ids in case event is at another business | |
foreach($businesses as $business) { | |
$venue_ids [$business->id] = $business->facebook; | |
} | |
foreach($businesses as $business) { | |
$facebook_id = preg_replace('#pages\\/[^/]+/#', '', $business->facebook); | |
$request = new FacebookRequest ( | |
$session, | |
'GET', | |
'/' . $facebook_id . '/events', | |
array('fields' => 'name,cover,description,place,start_time,end_time,is_date_only') | |
); | |
try { | |
$fb_events = $request->execute()->getGraphObject()->asArray()['data']; | |
if (is_null ($fb_events)) continue; | |
foreach ($fb_events as $fb_event) { | |
$new = false; | |
$event = Event::query() | |
->where ('fb_id', $fb_event->id) | |
->single (); | |
if (!$event) { | |
$event = new Event; | |
$event->pending = 1; | |
$new = true; | |
} | |
// Update event if it's pending; otherwise stick with what we've got | |
if ($event->pending) { | |
$event->business_id = $business->id; | |
$event->fb_id = $fb_event->id; | |
$event->venue_id = intval (array_search ($fb_event->place->id, $venue_ids)); | |
$event->title = isset($fb_event->name) ? $fb_event->name : ''; | |
$event->details = isset ($fb_event->description) ? $fb_event->description : ''; | |
$event->start_date = isset($fb_event->start_time) ? date('Y-m-d', strtotime($fb_event->start_time)) : ''; | |
$event->starts = isset($fb_event->start_time) ? date('H:i:s', strtotime($fb_event->start_time)) : ''; | |
$event->end_date = isset($fb_event->end_time) ? date('Y-m-d', strtotime($fb_event->end_time)) : $event->start_date; | |
$event->ends = isset($fb_event->end_time) ? date('H:i:s', strtotime($fb_event->end_time)) : $event->starts; | |
$event->email = $business->email; | |
if (isset($fb_event->place)) { | |
$event->address = isset($fb_event->place->location->street) ? $fb_event->place->location->street : ''; | |
$event->city = isset($fb_event->place->location->city) ? $fb_event->place->location->city : ''; | |
$event->state = isset($fb_event->place->location->state) ? $fb_event->place->location->state : ''; | |
$event->zip = isset($fb_event->place->location->zip) ? $fb_event->place->location->zip : ''; | |
if (isset ($fb_event->place->location->latitude)) | |
$event->latitude = $fb_event->place->location->latitude; | |
if (isset ($fb_event->place->location->longitude)) | |
$event->longitude = $fb_event->place->location->longitude; | |
$event->venue = isset($fb_event->place->name) ? $fb_event->place->name : ''; | |
} | |
if (isset($fb_event->cover->source)) { | |
$image = file_get_contents($fb_event->cover->source); | |
$matches = []; | |
$match = preg_replace('#\?.+#', '', $fb_event->cover->source); | |
preg_match('#\.[^.]+$#', $match, $matches); | |
$filename = 'files/events/' . $fb_event->cover->id . $matches[0]; | |
file_put_contents($filename, $image); | |
$event->thumbnail = $filename; | |
} | |
$event->put(); | |
if ($event->error) { | |
info ($event->error); | |
info (DB::$last_sql); | |
info (DB::$last_args); | |
} else { | |
if ($new) info ("Added " . $event->title); | |
else info ("Updated " . $event->title); | |
} | |
} | |
} | |
} catch (Exception $e) { | |
echo 'Caught exception: ', $e->getMessage(), '; facebook id is ', $facebook_id, "<br>"; | |
echo "\n"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment