Jake McMurchie saxophonist and Wordpress developer

Jake McMurchie

Using the Google Calendar API to create a standalone file you can include as a listing.

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. In the end it’s been easier to keep the gigs in the calendar (a Google Calendar) as I normally would and then create a parallel “live” calendar. When a gig is confirmed I copy it across to the “live” calendar and it’s automatically published on the website. Basically, all I have to worry about is Google Calendar – which I manage from the laptop or the phone – and the website takes care of itself.

Initially I used the javascript API, but then I read this article (more of this another time) 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.

The process

Create the Google Calendar project

Login to your Google account and follow the instructions on this page. They start by asking you to follow this wizard.

  1. Click “continue”
  2. Click “go to credentials”
  3. On the “Add credentials to your project” page click “cancel”
  4. 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.
  5. If you’re not already on it, select the “Credentials” tab, click the “Create credentials” button and select “OAuth client ID”.
  6. Select the application type Other, enter the name “Google Calendar API Quickstart”, and click the “Create” button.
  7. Click “OK” to dismiss the resulting dialog.
  8. Click the download_json (Download JSON) button to the right of the client ID and save this file, renaming it client_secret.json.

Set up the Google Calendar PHP API client

  1. Download the API client files from Github. You can do this by cloning the git repository but I’m happier downloading the zip file
  2. Uncompress the zip file you download, rename it “google-calendar”
  3. Move client_secret.json into the google-calendar folder
  4. Create a new file in the google-calendar folder called “quickstart.php” and copy and paste the code on this page into it
  5. Upload to your webserver
  6. Log into your server over SSH, cd to the google-calendar folder and type “php quickstart.php”, and hit the “enter” key
  7. Copy and paste the link provided into your web-browser and authenticate with Google
  8. 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.

  1. 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”
  2. Scroll down to “integrate calendar” and copy the string under “Calendar ID”
  3. 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);


$out = '';

if (!empty($results->getItems())) {
	foreach ($results->getItems() as $event) {
		$start = $event->start->dateTime;
		if (empty($start)) {
			$start = $event->start->date;
		$date = substr($start, 0, 4) . "." . substr($start, 5, 2) . "." . substr($start, 8, 2);
		$summary = $event->getSummary();
		$desc = $event->description;
		if ($desc)
			$desc = "\n<br />$desc";
		$location = $event->location;
		if ($location)
			$location = "\n<br />$location";

		$out .= "<h3>$date</h3>\n<p><strong>$summary</strong>$location$desc</p>\n";

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:

  1. Log in to https://platform.cloudways.com
  2. Click “applications” and then clicked on my application
  3. Under “application management” I clicked “cron job management”, then “add new cron job”
  4. Under “common settings” I:
    1. selected “once a day”
    2. set 2 for the hour (so it’s run at 2am)
    3. made sure type was set to “PHP”
    4. 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:

0  2  *  *  *       cd /path/to/google-calendar/ && php quickstart.php  #CloudwaysApps

Include the file in your website

Finally, all you need is to include the file on a webpage by using:

<?php include('includes/gig-calendar.php'); ?>