Created
November 8, 2018 21:46
-
-
Save dasginganinja/3e68029e2449147fb677c41b7f8baaa3 to your computer and use it in GitHub Desktop.
Use google client api library to fetch events for display on website
This file contains hidden or 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 | |
define("HOURS_LIBRARY", "[email protected]"); | |
$path = '/path/to/google-api-php-client'; | |
require_once($path . "/vendor/autoload.php"); | |
function hours_get_data_for($calendar_id, $service, $opt_params) { | |
$events = $service->events->listEvents($calendar_id, $opt_params); | |
$todays_events = array(); | |
foreach ($events->getItems() as $event) { | |
// Get a list of events to output | |
// Add them to the todays events if they fit your criteria | |
if (is_valid_event($event)) { | |
$todays_events[] = $event; | |
} | |
} | |
$output = theme_hours_todays_events_list($todays_events); | |
return $output; | |
} | |
function is_valid_event($event) { | |
// use your own logic here. | |
// We have ours set as all day events on the calendar and we just check for that | |
return TRUE: | |
} | |
function theme_hours_todays_events_list($todays_events) { | |
$output = ''; | |
$output .= '<ul>'; | |
if (count($todays_events)) { | |
foreach ($todays_events as $event) { | |
$output .= '<li>' . $event . '</li>'; | |
} | |
} else { | |
$output .= '<li>No information available</li>'; | |
} | |
$output .= '</ul>'; | |
return $output; | |
} | |
try { | |
$developerkey = '<your-key-here>'; | |
$client = new Google_Client(); | |
$client->setApplicationName("lts-hours"); | |
$client->setDeveloperKey($developerkey); | |
$service = new Google_Service_Calendar($client); | |
$opt_params = array( | |
'timeMin' => date(DateTime::ATOM, mktime(5,5,5)), | |
'timeMax' => date(DateTime::ATOM, mktime(18,59,59)), | |
'singleEvents' => TRUE, | |
); | |
// Query Google and generate the output we desire | |
$output = hours_get_data_for(HOURS_LIBRARY, $service, $opt_params); | |
} catch (\Exception $e) { | |
// handle exceptions here | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment