Last active
June 23, 2018 14:58
-
-
Save anthonyholmes/4942986 to your computer and use it in GitHub Desktop.
PHP Class for Accessing Data from Google Docs Spreadsheet
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 | |
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