Skip to content

Instantly share code, notes, and snippets.

@anthonyholmes
Last active June 23, 2018 14:58
Show Gist options
  • Save anthonyholmes/4942986 to your computer and use it in GitHub Desktop.
Save anthonyholmes/4942986 to your computer and use it in GitHub Desktop.
PHP Class for Accessing Data from Google Docs Spreadsheet
<?php
Class DocDB {
private $key;
private $url;
private $file;
private $json;
private $rows;
private $data = array();
private $columns = array();
private $db_row = array();
public function __construct($key, $columns = array()){
$this->key = $key;
$this->url = 'http://spreadsheets.google.com/feeds/list/' . $this->key . '/od6/public/values?alt=json';
$this->file = file_get_contents($this->url);
$this->json = json_decode($this->file);
$this->rows = $this->json->{'feed'}->{'entry'};
$this->columns = $columns;
$this->collect();
}
public function toArray(){
return $this->data;
}
public function toJSON(){
return json_encode($this->data);
}
private function collect(){
foreach($this->rows as $row) {
foreach ($this->columns as $key) {
$this->db_row[$key] = $row->{'gsx$'.$key}->{'$t'} ;
}
array_push($this->data, $this->db_row);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment