How to Use Google Sheets as a Database for Your PHP App
Time to read:
How to Use Google Sheets as a Database for Your PHP App
As developers, we're not short of options when it comes to databases, whether commercial or open source, for making our application data available to business users for further analysis. For example, you're likely already familiar with MySQL, Postgres, Microsoft SQL Server, Oracle, SAP, as well as so many others.
But, do business users need all of the functionality that databases offer?
It's easy to assume so. However, what if the business needed something lighter, more familiar, and more readily accessible? What if, as is still so common — sometimes ubiquitous, even — all the business wanted was a spreadsheet?
Before you change tabs, hear me out. Spreadsheets have been around (admittedly, not in electronic form) since at least 1906, and in electronic form since VisiCalc debuted on the Apple II way back in 1979; which in turn was followed by Lotus 1-2-3 and Microsoft Excel.
Whether we, as developers, like them or not, they're a mainstay of organisations, both large and small, in so many countries around the world, regardless of industry sector. And, for good reason! They're easy to use, quick to get started with, don't require a steep learning curve (at least initially), and make data relatively easy to manipulate, analyse, and visualise.
So, why not use them in PHP applications to store application data for analysis by business users?
However, before you think I'm talking about Microsoft Excel. I'm not. I'm talking about its direct competitor: Google Sheets. It's easy to start using, easy to collaborate on with others, whether inside or outside of an organisation, has in-built version control, plus so much more; oh, and it costs nothing to get started.
But, as good as Google Sheets is, it has no native notification functionality. Notifications would be helpful when you want to notify users and stakeholders that more data is available to analyse for that next, last minute report.
So, in this tutorial, you're going to learn how to backup data from Order Desk, an ecommerce order management tool, to Google Sheets and notify users afterward via SMS.
Prerequisites
Before you begin, make sure you have the following:
- A Twilio account (free or paid). Sign up today if you don't have an account.
- An Order Desk account
- A Google account, with a Google Project which has the Google Sheets API enabled
- PHP 8.4 or above
- Composer installed globally
- curl, or an equivalent network tool such as wget or Resterm
- Your preferred code editor or IDE, such as Neovim or Visual Studio Code
- Some familiarity with the terminal would be helpful, though isn't required
Application overview
The application will be quite simple; a small, web-based application with a single route. When that route is requested, the route's handler will retrieve all of the current orders from your Order Desk account and write them to a Google Sheet. After that, thanks to the power of Twilio's Programmable Messaging API, you'll be notified that new data is available via SMS.
Set up the project directory
The first thing that we need to do is to set up the project directory structure, by running the following commands.
If you're using Microsoft Windows, use the commands below, instead.
Here's what's going on:
- The data directory will store your Google Service Account's credentials file (credentials.json)
- The public directory will store the application's bootstrap file (index.php) where all requests will be routed
- The src/App directory will store the application's PSR-4 namespaced code files, in a namespace, unoriginally named "App". The App directory contains two subdirectories: Handler for handler classes, and Service for some service, utility classes to simplify interacting with Order Desk and Google Sheets.
Install the required packages
Next, we need to install the application's PHP dependencies. These are:
To install the packages, run the following command.
Then, add the following to the newly generated composer.json file to register a PSR-4 autoload namespace named "App".
Set the required environment variables
To communicate with Order Desk, Google Sheets, and Twilio, the application will need a series of environment variables; these are:
Paste the following into a new file named .env in the project's top-level directory.
Then, add your phone number in E.164 format as the value of RECIPIENT.
Now, let's retrieve all of the required credentials.
Retrieve your Twilio credentials and phone number
You need to retrieve your Twilio credentials, and your Twilio phone number, so that. With them, the app can make authenticated requests to Twilio's APIs, and Twilio knows who's sending the SMS.
To retrieve them, log in to your Twilio Console. There, from the Account Info panel copy your Account SID, Auth Token, and phone number and paste them into .env as the values for TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN, and TWILIO_PHONE_NUMBER, respectively.
Retrieve your Google credentials
Next up, you need to retrieve your Google credentials. It's a little involved, but bear with me. Log in to your Google Cloud Console, open the navigation menu (the three horizontal bars in the top-left hand corner), and open IAM and admin > Service accounts. There, click Create service account.
Then, fill in the details for "Service account name" and click Done. I named mine "Twilio Sheets", but feel free to name yours whatever you prefer.
After that, click the Action menu on the far right of your new service account, and click Manage keys.
Then, click Add key > Create new key. In the popup that appears, leave "Key type" set to "JSON" and click Create.
A JSON file will then start downloading. When it's downloaded, move it to the project's data directory and rename it credentials.json. Then, set the absolute path to credentials.json as the value of GOOGLE_APPLICATION_CREDENTIALS in .env.
After that, go back to your Google Cloud Console and reload it. Now, you'll see a key in the table at the bottom of the page. Copy the value in the Key column and paste it into .env as the value of GOOGLE_API_KEY.
Then, go to the Details tab of your new service account and copy the email address to .env as the value of GOOGLE_SERVICE_ACCOUNT_NAME.
Retrieve your Order Desk credentials
To retrieve them, log in to your Order Desk account. Then, go to Settings > Store Settings, and click the API tab.
Then, click Create API Key.
You'll then see your Store ID and API Key. Copy and paste them into .env in place of ORDERDESK_STORE_ID and ORDERDESK_API_KEY, respectively.
While you're there, create at least one new order, so that the PHP app has some data to retrieve. To do that, in the left-hand side navigation menu, click "Add New Order" under TOOLS.
Fill in as many details as possible, making sure that you fill in Shipping Method, Shipping Total and Tax Total, and Payment Status, along with your email address and first and last names.
Create the PHP application
With all of the application setup completed, it's now time to write the PHP code. Start by creating a new file named OrderDeskService.php in src/App/Service. Then, paste the code below into the file.
The above code is a minimally refactored version of Order Desk's PHP client, so that it better complies with PHP 8.4. It simplifies making authenticated API requests to Order Desk's API and validates the responses returned. There are packages available on Packagist, but I found this class to be sufficient.
With that done, create a second file, this time in src/App/Service, named GoogleSheetsService.php. In the file, paste the code below.
The class' constructor initialises a new Google API PHP Client object (which provides low-level access to Google's APIs) using your API key and service account's name. Its scope is limited to only interacting with Google Sheets. Then, two further functions are defined:
addRows()appends one or more rows ($values) to a Google Sheet identified by$spreadsheetId, at the range defined by$rangegetRows()returns all of the current rows in a Google Sheet with the id in$spreadsheetId. These two functions are used together to determine where to append new rows to the Google Sheet as well as the new rows to append.
After that, create a blank Google Sheet. Then, copy its unique id and set it as the value of SPREADSHEET_ID in .env. Its unique id is in the Sheet's URL between "/d/" and "/edit?gid=0#gid=0".
Following that, in the Google Sheet label columns A through F with the following column headers:
Here's an example of what it should look like.
Then, near the top right-hand corner, click Share. Then, under General Access, change "Restricted" to "Anyone with the link", change the role from "Viewer" to "Editor", then click Done.
Next, create a file named TwilioSheetsHandler.php in src/App/Handler. Then, paste the code below into the file.
This is the omnibus class, invoked when the application's default route is requested. The class' constructor takes instances of the three classes that we've defined so far, and an array of configuration options containing the recipient's phone number, your Twilio phone number, and the Google Sheet's unique id.
The __invoke() magic method, invoked when the application's default route is called:
- Retrieves all orders from your Order Desk account
- Extracts the order information
- Calls
addOrdersToGoogleSheets()to write them to the Google Sheet - Calls
sendSms()to send an SMS notification to the recipient
The addOrdersToGoogleSheets() function uses the GoogleSheetsService instance to write the supplied orders to the Google Sheet. If there are no rows, it writes the orders to the Sheet starting on the second row, so that the header cells are not overwritten. Otherwise, it appends them after the existing orders in the Sheet.
The sendSms() function uses the Twilio Rest Client to send an SMS to the recipient from your Twilio phone number, with the message: "Hey! %d orders have been added to your Google Sheet!", where "%d" is replaced by the number of orders retrieved from your Order Desk account.
Finally, create a fourth file, named index.php, this time in the public directory. Then, paste the code below into the file.
This file is the core of the application, its bootstrap file, where all requests are initially sent. It starts by initialising the GoogleSheetsService, OrderDeskService, and TwilioRestClient objects.
Following that, it initialises a new Slim Framework App instance, the central instance of a Slim Framework application, and registers a single route ("/update") in the application's routing table. That route is handled by, as previously mentioned, the TwilioSheetsHandler's __invoke() magic method.
Test that the application works as expected
Now that the application's built, it's time to start it and test it. Start the application running using PHP's in-built webserver, by running the command below.
Then, in a separate terminal session or tab, run the following command to call the application's sole endpoint.
When the request is completed, you should see "Retrieved %d orders" printed to the terminal, where "%d" is replaced by the total number of orders in your Order Desk account.
Take a look at your Google Sheet, where you'll find that it's populated with the order(s) from your Order Desk account, as in the screenshot below.
Now, check your phone to see that you've received the SMS notification, telling you that your Order Desk orders have been added to your Google Sheet.
That's how to backup data in Google Sheets in your PHP applications
While there are a number of, proverbial, moving parts, there wasn't a lot of code to write to integrate Order Desk, Google Sheets, and Twilio into a small, PHP-powered web application.
I hope that this tutorial has shown you how easy it is to backup an online service, such as Order Desk, using PHP and Google Sheets as your backup tool, and send post-backup notifications using Twilio's infrastructure.
What service does your application integrate with, and where would you back it up to? Also, what kind of customer notifications would you send?
What's Next?
If you're keen to go further and keep on learning, how about sending an MMS or WhatsApp message instead of an SMS. Finally, here is a selection of resources to continue growing your knowledge:
- Order Desk's API documentation
- Twilio's Programmable Messaging documentation
- The Google Sheets documentation
- Best practices for managing service account keys
Matthew Setter is (primarily) the PHP, Go, and Rust editor on the Twilio Voices team. He’s also the author of Mezzio Essentials and Deploy with Docker Compose. You can find him at msetter[at]twilio.com. He's also on LinkedIn and GitHub.
Related Posts
Related Resources
Twilio Docs
From APIs to SDKs to sample apps
API reference documentation, SDKs, helper libraries, quickstarts, and tutorials for your language and platform.
Resource Center
The latest ebooks, industry reports, and webinars
Learn from customer engagement experts to improve your own communication.
Ahoy
Twilio's developer community hub
Best practices, code samples, and inspiration to build communications and digital engagement experiences.