Прочитать JSON электронной таблицы Google


Всем привет, у меня проблема с PHP, пожалуйста, помогите мне в этом.

Я хочу прочитать данные из электронной таблицы Google с помощью PHP, и я поискал в Интернете и получил формат 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": "[email protected]" } } ], "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, он возвращает:

Пожалуйста, помогите мне в этом. Спасибо всем!

Author: Quill, 2014-10-08

2 answers

Обычно вы просто используете 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
Author: Ghost, 2014-10-08 00:49:17

json_decode($json_string, $assoc) это функция php, которая принимает строку JSON и возвращает ассоциативный массив, когда $assoc имеет значение true.
смотрите документы
вот пример:

<?php

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

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

?>
 0
Author: danyamachine, 2014-10-08 00:07:08