forked from benjisimon/code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheasy_google_spreadsheet_access.php
More file actions
62 lines (50 loc) · 1.58 KB
/
easy_google_spreadsheet_access.php
File metadata and controls
62 lines (50 loc) · 1.58 KB
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
<?
/*
* Demonstrate easy access to a Google Spreadsheet
*/
// Before getting into the code, you'll want to:
// (a) make sure the document is published to the web
// (b) figure out the worksheet URL by running something like
//
// curl -s 'https://spreadsheets.google.com/feeds/worksheets/[id]/public/full' | xmllint.exe -format -|\
// grep '2006#list' | sed -e 's/^.*href="//' -e '/".*//'
//
$worksheet_url = 'https://spreadsheets.google.com/feeds/list/1IR4JAaJWcScBONP4R3O_IbO4ecyVF5kOBEBzoExjM7E/od6/public/full';
/*
* OK, we've got our URL so slurp it in and make it an easily accessible
* document. Notice how the $gsx namespace is setup for easy use below.
*/
$ch = curl_init($worksheet_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$xml = curl_exec($ch);
$doc = new SimpleXMLElement($xml);
$ns = $doc->getNamespaces(true);
$gsx = $ns['gsx'];
/*
* Do something with the data.
*/
header("Content-Type: text/plain");
$nsamples = 0;
$max = false;
$avg = false;
foreach($doc->entry as $e) {
$nsamples++;
if($max === false) {
$max = array('ts' => $e->children($gsx)->timestamp . "",
'val' => $e->children($gsx)->pressure);
} else {
if(($e->children($gsx)->pressure . "") > $max['val']) {
$max['ts'] = $e->children($gsx)->timestamp . "";
$max['val'] = $e->children($gsx)->pressure . "";
}
}
if($avg === false) {
$avg = $e->children($gsx)->pressure . "";
} else {
$avg = (($e->children($gsx)->pressure . "") + $avg) / 2;
}
}
echo "Num Samples: $nsamples\n";
echo "Max: {$max['ts']} = {$max['val']}\n";
echo "Avg: $avg\n";
?>