Shoora
11/18/2015 - 6:46 AM

A simple way of getting the data from a Google Docs Spreadsheet in PHP

A simple way of getting the data from a Google Docs Spreadsheet in PHP

<?php
function get_google_spreadsheet($key) {
	$url = "https://spreadsheets.google.com/feeds/list/{$key}/od6/public/values";
	$google_sheet = file_get_contents($url);
	$xml = simplexml_load_string($google_sheet);

	$data = array();
	foreach($xml->entry as $entry) {
		$row = array();
		// The fields are in the gsx: namespace, so we need to specify that to be able to access them through SimpleXML.
		$fields = $entry->children('http://schemas.google.com/spreadsheets/2006/extended');
		foreach($fields as $id => $field)
			$row[$id] = (string) $field;		
		$data[] = $row;
	}
	
	return $data;
}

$key = "your-document-key";
$data = get_google_spreadsheet($key);
print_r($data);