Accessing Google Spreadsheet with PHP
Google spreadsheet can be used as a pseudo-database when setting up a more formal mySQL table is more than you’re looking for. I was looking to write a simple app that pulled in email addresses and sent a short email (it will be run on a monthly cron as a reminder). I wanted to easily be able to manage those email addresses. Google Spreadsheet was the ticket.
See my code below where I access the sheet, pull out the second row, and sends an email to any string the qualifies as an email address.
Consult the Google Spreadsheet API for more access methods. I am using the “cells” feed.
1: <?php
2:
3: $key = "THE KEY FOR YOUR SPREADSHEET";
4:
5: $url = "http://spreadsheets.google.com/feeds/cells/$key/1/public/values";
6:
7: $ch = curl_init();
8:
9: // set URL and other appropriate options
10: curl_setopt($ch, CURLOPT_URL, $url);
11: curl_setopt($ch, CURLOPT_HEADER, 0);
12: curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
13:
14: // grab URL and pass it to the browser
15: $google_sheet = curl_exec($ch);
16:
17: // close cURL resource, and free up system resources
18: curl_close($ch);
19:
20: $doc = new DOMDocument();
21: $doc->loadXML($google_sheet);
22:
23: $nodes = $doc->getElementsByTagName("cell");
24:
25: if($nodes->length > 0)
26: {
27: foreach($nodes as $node)
28: {
29: // 2nd row is the email row.
30: if ($node->getAttribute("row") == 2)
31: {
32: if (eregi("^[\.\+_a-z0-9-]+@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,6})$", $node->nodeValue) )
33: {
34: mail($node->nodeValue, "Mail Subject", "Mail Message.", "From: email@yourdomain.com");
35: }
36:
37: }
38: }
39: }
40:
41: ?>
In order to access the sheet with PHP I had to set the sheet to “public”. There is probably a way to create a secure connection with an Auth Token, but that was the not the purpose of this exercise. The key for your spread sheet can be found in the URL of the sheet when you have it open in the browser.















9 Comments, Comment or Ping
Rob
When I try this with my Google Spreadsheet, I get an error:
"You do not have view access to the spreadsheet. Make sure you are properly authenticated."
Did you have to do any special authentification before you were able to pull data out?
Dec 11th, 2008
cwpollock
@Rob,
Unfortuantely I had to mark my document "public" for it to work. I noted that below the coded section in the blog post. Otherwise I'm guessing you have to implement one of the authentication methods.
Dec 11th, 2008
Stephen Akins
Nice, simple introductory article!
Readers may be interested in the following article also:
http://stephenakins.blogspot.com/2009/04/google-docs-server-monitoring_8546.html
It uses the same method to create a server monitor, using the Google Spreadsheet as it's front end.
Again, nice work!
Dec 5th, 2009
David Smyth
Hi,
I'm just researching how easy it would be to read a google spreadsheet into a mysql database. This looks to be exactly what I was after, except for one issue for me…
I have recently tried using curl on my webhost but apparently the module is disabled. Is there a way of doing this without curl? If not, do you know how I can enable curl through .htaccess? I've not been able to find this anywhere.
Kind Regards,
David.
Feb 4th, 2010
Itlan
David Smyth, I have used this website with success:
http://farinspace.com/2009/05/saving-form-data-to-google-spreadsheets/
You need both the author's helper class, and a Zend Framework library. See website. Email me if you have questions.
Feb 8th, 2010
Dimas
David, if you don't have access to CURL, perhaps see if Snoopy will work for you … http://sourceforge.net/projects/snoopy/
I think it uses CURL if enabled else it uses plain sockets…
Mar 2nd, 2010
Alex
Great! This is very useful info. Love Google Docs, especially Spreadsheets. Thank you
Apr 18th, 2010
Will
There should be an option to copy the code without line numbers. This is pretty annoying, but otherwise this is helpful.
May 24th, 2010
Erik
Nice script! I found out that for simple tables reading the public CSV-output of the spreadsheet with fgetcsv() is an easier option. I use that to display comments but this XML-solution is more solid and probably way better in terms of OO, portability etc.
May 28th, 2010
Reply to “Accessing Google Spreadsheet with PHP”