Skip to content

Instantly share code, notes, and snippets.

@dasginganinja
Created November 8, 2018 21:46
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 dasginganinja/3e68029e2449147fb677c41b7f8baaa3 to your computer and use it in GitHub Desktop.
Save dasginganinja/3e68029e2449147fb677c41b7f8baaa3 to your computer and use it in GitHub Desktop.
Use google client api library to fetch events for display on website
<?php
define("HOURS_LIBRARY", "your-calendar-group-id@group.calendar.google.com");
$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