Skip to content

Instantly share code, notes, and snippets.

@atestu
Last active September 24, 2017 07:07
Show Gist options
  • Save atestu/030f171c227c2ede2c4f to your computer and use it in GitHub Desktop.
Save atestu/030f171c227c2ede2c4f to your computer and use it in GitHub Desktop.
Use Google Sheets as a micro CMS in PHP
<?php
// based on http://sim.plified.com/2008/09/14/accessing-google-spreadsheet-with-php/
function cbi_get_spreadsheet ($key) {
$url = "http://spreadsheets.google.com/feeds/cells/$key/1/public/values";
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
// grab URL and pass it to the browser
$google_sheet = curl_exec($ch);
// close cURL resource, and free up system resources
curl_close($ch);
$doc = new DOMDocument();
$doc->loadXML($google_sheet);
$cells = $doc->getElementsByTagName("cell");
$rows = array();
$headers = array();
$currentObject = array();
if($cells->length > 0)
{
foreach($cells as $cell)
{
if ($cell->getAttribute("row") != 1) {
$currentObject[$headers[$cell->getAttribute('col')-1]] = $cell->nodeValue;
if ($cell->getAttribute('col') == count($headers))
array_push($rows, $currentObject);
}
else {
array_push($headers, $cell->nodeValue);
}
}
}
return $rows;
}
<?php
require('get_sheet.php');
function cbi_get_press () {
return cbi_get_spreadsheet("YOUR_KEY_HERE");
}
@atestu
Copy link
Author

atestu commented Sep 5, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment