Last active
September 24, 2017 07:07
-
-
Save atestu/030f171c227c2ede2c4f to your computer and use it in GitHub Desktop.
Use Google Sheets as a micro CMS in PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require('get_sheet.php'); | |
function cbi_get_press () { | |
return cbi_get_spreadsheet("YOUR_KEY_HERE"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
based on Accessing Google Spreadsheet with PHP