-
Notifications
You must be signed in to change notification settings - Fork 2
/
GetSpreadsheetData.class.php
67 lines (51 loc) · 1.27 KB
/
GetSpreadsheetData.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<?php
class GetSpreadsheetData {
private $spreadsheetId;
private $url;
public function __construct($spreadsheetId) {
if(!$spreadsheetId) {
print ("Argument missing. You must provide a spreadsheet ID.");
return false;
}
$this->spreadsheetId = $spreadsheetId;
$this->url = "https://spreadsheets.google.com/feeds/list/". $this->spreadsheetId ."/od6/public/values?alt=json";
$this->init();
}
private function init() {
// Set character encoding
mb_internal_encoding("UTF-8");
}
private function getData() {
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $this->url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
public function returnData() {
$data = $this->getData();
// Check if the data returned is valid JSON.
if (!is_object(json_decode($data))) {
echo $data;
return false;
}
$json = json_decode($data);
$rows = $json->{'feed'}->{'entry'};
return $rows;
}
private function viewRawData() {
$data = $this->returnData();
if($data) {
echo "<pre>";
print_r($data);
echo "</pre>";
}
}
public function __toString() {
return (string)$this->viewRawData();
}
}
?>