Using the Google Calendar API to integrate a Google calendar into your website
12/06/2018
For gig listings I’ve used a number of different methods to put them on the website: a WordPress event plugin, a custom plugin I’ve written myself, a Bandsintown widget, a Songkick widget, … but all have fallen by the wayside because they require a little too much additional work.
So here’s what I do now:
I keep track of my gig offers in my calendar, a Google Calendar. When a gig is confirmed I copy it across to a separate “confirmed gigs” calendar (also a Google Calendar). A script in my webspace uses the Google Calendar API to get the dates from the “confirmed gigs” calendar, writes them to a file, which my website includes. I can then either set up a cron job to run this script at regular intervals to update the file, or I can run the file manually (which allows me to keep recent gigs displayed on the website after the date).
Initially I used the javascript API, but then I read this article and I decided that the resources would be better concentrated on the server than the browser. I’m running a WordPress website, so I needed to do this via PHP.
On the “Add credentials to your project” page click “cancel”
At the top of the page, select the “OAuth consent screen” tab. Select an Email address, enter a Product name if not already set, and click the “Save” button.
If you’re not already on it, select the “Credentials” tab, click the “Create credentials” button and select “OAuth client ID”.
Select the application type Other, enter the name “Google Calendar API Quickstart”, and click the “Create” button.
Click “OK” to dismiss the resulting dialog.
Click the (Download JSON) button to the right of the client ID and save this file, renaming it client_secret.json.
Uncompress the zip file you download, rename it “google-calendar”
Move client_secret.json into the google-calendar folder
Create a new file in the google-calendar folder called “quickstart.php” and copy and paste the code on this page into it
Upload to your webserver
Log into your server over SSH, cd to the google-calendar folder and type “php quickstart.php”, and hit the “enter” key
Copy and paste the link provided into your web-browser and authenticate with Google
Copy the paste the code displayed in your web-browser back into the SSH window and hit “enter”
Get the data from your Google Calendar and write it to a file
At the bottom of quickstart.php you’ll see this:
// Print the next 10 events on the user's calendar.
$calendarId = 'primary';
$optParams = array(
'maxResults' => 10,
'orderBy' => 'startTime',
'singleEvents' => true,
'timeMin' => date('c'),
);
$results = $service->events->listEvents($calendarId, $optParams);
if (empty($results->getItems())) {
print "No upcoming events found.\n";
} else {
print "Upcoming events:\n";
foreach ($results->getItems() as $event) {
$start = $event->start->dateTime;
if (empty($start)) {
$start = $event->start->date;
}
printf("%s (%s)\n", $event->getSummary(), $start);
}
}
We have a primary calendar which we use to schedule rehearsals, unavailability, all sorts of stuff. As explained above we have a separate calendar for public events (primarily gigs) called “Get The Blessing Live”. I only want to pull from this calendar.
Go back to your Google Calendar, under “my calendars” on the left, hover over the alternative calendar and click on the 3 dots to access the options. Click “settings and sharing”
Scroll down to “integrate calendar” and copy the string under “Calendar ID”
Paste this into quickstart.php line 82 to replace the “primary” ID
Note also that you can change the “maxResults” label here if you want to display more than 10 forthcoming events.
Now replace lines 91 to 102:
if (empty($results->getItems())) {
print "No upcoming events found.\n";
} else {
print "Upcoming events:\n";
foreach ($results->getItems() as $event) {
$start = $event->start->dateTime;
if (empty($start)) {
$start = $event->start->date;
}
printf("%s (%s)\n", $event->getSummary(), $start);
}
}
This gets more data about the event and formats the date. Note the formatting of each event is determined by the construction of the last line. $out is our variable storing up all the events.
Now we add the following to write the events into a PHP file one level up, parallel to the google-calendar folder:
$success = file_put_contents("../gig-calendar.php", $out);
if (!$success)
mail('jake@www.jakemcmurchie.net', 'cron failed', 'sorry');
If the file isn’t created or replaced, the system will email me to apologise. Motto for life: even if you’re a failure, be polite.
Set up a cron job to refresh the gig list every day
I host with Cloudways and this was more complicated than I initially thought it would be. But not much.
I started by doing the following:
Log in to https://platform.cloudways.com
Click “applications” and then clicked on my application
Under “application management” I clicked “cron job management”, then “add new cron job”
Under “common settings” I:
selected “once a day”
set 2 for the hour (so it’s run at 2am)
made sure type was set to “PHP”
typed in the path to quickstart.php
This didn’t work initially but a conversation with Cloudways’ helpful tech support told me that I needed to switch to the “advanced” tab and include a cd to the working directory first. So the cron job code ended up looking like this: