阅读Google电子表格的JSON

时间:2014-10-07 23:56:10

标签: php arrays json google-sheets

大家好,我有PHP问题,请帮我解决这个问题。

我想使用PHP从Google电子表格中读取数据,我搜索了一些网页并获得了JSON格式的表格。但我无法用PHP读取我的数据。

这是我的PHP代码:

<?php

echo "<h1>Hello, PHP!</h1>";
$url = "https://spreadsheets.google.com/feeds/list/0Aqglj65pqAwmdEh4a1otT3lmYnN0TGV1Q2JkdndVUnc/od6/public/basic?hl=en_US&alt=json";
$data = file_get_contents($url);
echo $data;

/*What can I do here to read the values of Sensor1,Sensor2,Sensor3 in result ? */

?>

这就是结果:

 { "version": "1.0", "encoding": "UTF-8", "feed": { "xmlns": "http://www.w3.org/2005/Atom", "xmlns$openSearch": "http://a9.com/-/spec/opensearchrss/1.0/", "xmlns$gsx": "http://schemas.google.com/spreadsheets/2006/extended", "id": { "$t": "https://spreadsheets.google.com/feeds/list/1X9ofybaA0PiXE-rCec-m1CuNgkPENqWFogWjgoVXZRc/od6/public/values" }, "updated": { "$t": "2014-10-07T22:49:06.978Z" }, "category": [ { "scheme": "http://schemas.google.com/spreadsheets/2006", "term": "http://schemas.google.com/spreadsheets/2006#list" } ], "title": { "type": "text", "$t": "Sheet1" }, "link": [ { "rel": "alternate", "type": "application/atom+xml", "href": "https://docs.google.com/spreadsheets/d/1X9ofybaA0PiXE-rCec-m1CuNgkPENqWFogWjgoVXZRc/pubhtml" }, { "rel": "http://schemas.google.com/g/2005#feed", "type": "application/atom+xml", "href": "https://spreadsheets.google.com/feeds/list/1X9ofybaA0PiXE-rCec-m1CuNgkPENqWFogWjgoVXZRc/od6/public/values" }, { "rel": "http://schemas.google.com/g/2005#post", "type": "application/atom+xml", "href": "https://spreadsheets.google.com/feeds/list/1X9ofybaA0PiXE-rCec-m1CuNgkPENqWFogWjgoVXZRc/od6/public/values" }, { "rel": "self", "type": "application/atom+xml", "href": "https://spreadsheets.google.com/feeds/list/1X9ofybaA0PiXE-rCec-m1CuNgkPENqWFogWjgoVXZRc/od6/public/values?alt\u003djson" } ], "author": [ { "name": { "$t": "lethanhtrieuK36" }, "email": { "$t": "lethanhtrieuk36@gmail.com" } } ], "openSearch$totalResults": { "$t": "9" }, "openSearch$startIndex": { "$t": "1" }, "entry": [ { "id": { "$t": "https://spreadsheets.google.com/feeds/list/1X9ofybaA0PiXE-rCec-m1CuNgkPENqWFogWjgoVXZRc/od6/public/values/cokwr" }, "updated": { "$t": "2014-10-07T22:49:06.978Z" }, "category": [ { "scheme": "http://schemas.google.com/spreadsheets/2006", "term": "http://schemas.google.com/spreadsheets/2006#list" } ], "title": { "type": "text", "$t": "10/4/2014 15:56:01" }, "content": { "type": "text", "$t": "sensor1: demo data, sensor2: ON, sensor3: NOW" }, "link": [ { "rel": "self", "type": "application/atom+xml", "href": "https://spreadsheets.google.com/feeds/list/1X9ofybaA0PiXE-rCec-m1CuNgkPENqWFogWjgoVXZRc/od6/public/values/cokwr" } ], "gsx$timestamp": { "$t": "10/4/2014 15:56:01" }, "gsx$sensor1": { "$t": "demo data" }, "gsx$sensor2": { "$t": "ON" }, "gsx$sensor3": { "$t": "NOW" } }, { "id": { "$t": "https://spreadsheets.google.com/feeds/list/1X9ofybaA0PiXE-rCec-m1CuNgkPENqWFogWjgoVXZRc/od6/public/values/cpzh4" }, "updated": { "$t": "2014-10-07T22:49:06.978Z" }, "category": [ { "scheme": "http://schemas.google.com/spreadsheets/2006", "term": "http://schemas.google.com/spreadsheets/2006#list" } ], "title": { "type": "text", "$t": "10/4/2014 15:56:05" }, "content": { "type": "text", "$t": "sensor1: 22, sensor2: OFF, sensor3: DO" }, "link": [ { "rel": "self", "type": "application/atom+xml", "href": "https://spreadsheets.google.com/feeds/list/1X9ofybaA0PiXE-rCec-m1CuNgkPENqWFogWjgoVXZRc/od6/public/values/cpzh4" } ], "gsx$timestamp": { "$t": "10/4/2014 15:56:05" }, "gsx$sensor1": { "$t": "22" }, "gsx$sensor2": { "$t": "OFF" }, "gsx$sensor3": { "$t": "DO" } }, { "id": { "$t": "https://spreadsheets.google.com/feeds/list/1X9ofybaA0PiXE-rCec-m1CuNgkPENqWFogWjgoVXZRc/od6/public/values/cre1l" }, "updated": { "$t": "2014-10-07T22:49:06.978Z" }, "category": [ { "scheme": "http://schemas.google.com/spreadsheets/2006", "term": "http://schemas.google.com/spreadsheets/2006#list" } ], "title": { "type": "text", "$t": "10/4/2014 15:56:05" }, "content": { "type": "text", "$t": "sensor1: 34, sensor2: ON, sensor3: SOME" }, "link": [ { "rel": "self", "type": "application/atom+xml", "href": "https://spreadsheets.google.com/feeds/list/1X9ofybaA0PiXE-rCec-m1CuNgkPENqWFogWjgoVXZRc/od6/public/values/cre1l" } ], "gsx$timestamp": { "$t": "10/4/2014 15:56:05" }, "gsx$sensor1": { "$t": "34" }, "gsx$sensor2": { "$t": "ON" }, "gsx$sensor3": { "$t": "SOME" } }, { "id": { "$t": "https://spreadsheets.google.com/feeds/list/1X9ofybaA0PiXE-rCec-m1CuNgkPENqWFogWjgoVXZRc/od6/public/values/chk2m" }, "updated": { "$t": "2014-10-07T22:49:06.978Z" }, "category": [ { "scheme": "http://schemas.google.com/spreadsheets/2006", "term": "http://schemas.google.com/spreadsheets/2006#list" } ], "title": { "type": "text", "$t": "10/4/2014 15:56:29" }, "content": { "type": "text", "$t": "sensor1: 33, sensor2: 27, sensor3: THING" }, "link": [ { "rel": "self", "type": "application/atom+xml", "href": "https://spreadsheets.google.com/feeds/list/1X9ofybaA0PiXE-rCec-m1CuNgkPENqWFogWjgoVXZRc/od6/public/values/chk2m" } ], "gsx$timestamp": { "$t": "10/4/2014 15:56:29" }, "gsx$sensor1": { "$t": "33" }, "gsx$sensor2": { "$t": "27" }, "gsx$sensor3": { "$t": "THING" } }, { "id": { "$t": "https://spreadsheets.google.com/feeds/list/1X9ofybaA0PiXE-rCec-m1CuNgkPENqWFogWjgoVXZRc/od6/public/values/ciyn3" }, "updated": { "$t": "2014-10-07T22:49:06.978Z" }, "category": [ { "scheme": "http://schemas.google.com/spreadsheets/2006", "term": "http://schemas.google.com/spreadsheets/2006#list" } ], "title": { "type": "text", "$t": "10/4/2014 15:56:47" }, "content": { "type": "text", "$t": "sensor1: MY, sensor2: 29, sensor3: undefined" }, "link": [ { "rel": "self", "type": "application/atom+xml", "href": "https://spreadsheets.google.com/feeds/list/1X9ofybaA0PiXE-rCec-m1CuNgkPENqWFogWjgoVXZRc/od6/public/values/ciyn3" } ], "gsx$timestamp": { "$t": "10/4/2014 15:56:47" }, "gsx$sensor1": { "$t": "MY" }, "gsx$sensor2": { "$t": "29" }, "gsx$sensor3": { "$t": "undefined" } }, { "id": { "$t": "https://spreadsheets.google.com/feeds/list/1X9ofybaA0PiXE-rCec-m1CuNgkPENqWFogWjgoVXZRc/od6/public/values/ckd7g" }, "updated": { "$t": "2014-10-07T22:49:06.978Z" }, "category": [ { "scheme": "http://schemas.google.com/spreadsheets/2006", "term": "http://schemas.google.com/spreadsheets/2006#list" } ], "title": { "type": "text", "$t": "10/4/2014 15:56:48" }, "content": { "type": "text", "$t": "sensor1: DATA, sensor2: WORK, sensor3: undefined" }, "link": [ { "rel": "self", "type": "application/atom+xml", "href": "https://spreadsheets.google.com/feeds/list/1X9ofybaA0PiXE-rCec-m1CuNgkPENqWFogWjgoVXZRc/od6/public/values/ckd7g" } ], "gsx$timestamp": { "$t": "10/4/2014 15:56:48" }, "gsx$sensor1": { "$t": "DATA" }, "gsx$sensor2": { "$t": "WORK" }, "gsx$sensor3": { "$t": "undefined" } }, { "id": { "$t": "https://spreadsheets.google.com/feeds/list/1X9ofybaA0PiXE-rCec-m1CuNgkPENqWFogWjgoVXZRc/od6/public/values/clrrx" }, "updated": { "$t": "2014-10-07T22:49:06.978Z" }, "category": [ { "scheme": "http://schemas.google.com/spreadsheets/2006", "term": "http://schemas.google.com/spreadsheets/2006#list" } ], "title": { "type": "text", "$t": "10/4/2014 15:56:53" }, "content": { "type": "text", "$t": "sensor1: 56, sensor2: DATABASE, sensor3: undefined" }, "link": [ { "rel": "self", "type": "application/atom+xml", "href": "https://spreadsheets.google.com/feeds/list/1X9ofybaA0PiXE-rCec-m1CuNgkPENqWFogWjgoVXZRc/od6/public/values/clrrx" } ], "gsx$timestamp": { "$t": "10/4/2014 15:56:53" }, "gsx$sensor1": { "$t": "56" }, "gsx$sensor2": { "$t": "DATABASE" }, "gsx$sensor3": { "$t": "undefined" } }, { "id": { "$t": "https://spreadsheets.google.com/feeds/list/1X9ofybaA0PiXE-rCec-m1CuNgkPENqWFogWjgoVXZRc/od6/public/values/cyevm" }, "updated": { "$t": "2014-10-07T22:49:06.978Z" }, "category": [ { "scheme": "http://schemas.google.com/spreadsheets/2006", "term": "http://schemas.google.com/spreadsheets/2006#list" } ], "title": { "type": "text", "$t": "10/4/2014 15:56:59" }, "content": { "type": "text", "$t": "sensor1: NUMBER, sensor2: 27, sensor3: undefined" }, "link": [ { "rel": "self", "type": "application/atom+xml", "href": "https://spreadsheets.google.com/feeds/list/1X9ofybaA0PiXE-rCec-m1CuNgkPENqWFogWjgoVXZRc/od6/public/values/cyevm" } ], "gsx$timestamp": { "$t": "10/4/2014 15:56:59" }, "gsx$sensor1": { "$t": "NUMBER" }, "gsx$sensor2": { "$t": "27" }, "gsx$sensor3": { "$t": "undefined" } }, { "id": { "$t": "https://spreadsheets.google.com/feeds/list/1X9ofybaA0PiXE-rCec-m1CuNgkPENqWFogWjgoVXZRc/od6/public/values/cztg3" }, "updated": { "$t": "2014-10-07T22:49:06.978Z" }, "category": [ { "scheme": "http://schemas.google.com/spreadsheets/2006", "term": "http://schemas.google.com/spreadsheets/2006#list" } ], "title": { "type": "text", "$t": "10/4/2014 15:56:59" }, "content": { "type": "text", "$t": "sensor1: 56, sensor2: CODE, sensor3: OK" }, "link": [ { "rel": "self", "type": "application/atom+xml", "href": "https://spreadsheets.google.com/feeds/list/1X9ofybaA0PiXE-rCec-m1CuNgkPENqWFogWjgoVXZRc/od6/public/values/cztg3" } ], "gsx$timestamp": { "$t": "10/4/2014 15:56:59" }, "gsx$sensor1": { "$t": "56" }, "gsx$sensor2": { "$t": "CODE" }, "gsx$sensor3": { "$t": "OK" } } ] } }

当我使用在线JSON Viewer时,它会返回:

请帮我解决这个问题。谢谢大家!

2 个答案:

答案 0 :(得分:1)

通常,您只需使用json_decode()来解析json字符串响应。要么使用true标志,要么只使用foreach来获得所需的结果:

echo "<h1>Hello, PHP!</h1>";
$url = "https://spreadsheets.google.com/feeds/list/0Aqglj65pqAwmdEh4a1otT3lmYnN0TGV1Q2JkdndVUnc/od6/public/basic?hl=en_US&alt=json";
$data = json_decode(file_get_contents($url), true);

$sensors = array();
foreach($data['feed']['entry'] as $entry) {
    foreach($entry as $content) {
        if(isset($content['$t']) && strpos($content['$t'], 'sensor') !== false) {
            $s = explode(',', $content['$t']);
            $temp = array();
            foreach($s as $line) {
                list($k, $v) = explode(': ', $line);
                $k = preg_replace('/\s+/', '', $k); trim($v); // cleaning
                $temp[$k] = $v;
            }

            $sensors[] = $temp;
        }
    }
}

echo '<pre>';
print_r($sensors);

应输出如下内容:

Array
(
    [0] => Array
        (
            [0] => sensor1: demo data
            [1] =>  sensor2: ON
            [2] =>  sensor3: NOW
        )

    [1] => Array
        (
            [0] => sensor1: 22
            [1] =>  sensor2: OFF
            [2] =>  sensor3: DO
        )
    ... and so on

答案 1 :(得分:0)

json_decode($json_string, $assoc)是一个php函数,它接受一个JSON字符串,并在$assoc设置为true时返回一个关联数组。
see the docs
这是一个例子:

<?php

$json = '{"foo-bar": 12345}';

$obj = json_decode($json);
print $obj->{'foo-bar'}; // will print 12345

?>