2014-10-07 96 views
2

大家好我有一个PHP的问题,请帮助我解决这个问题。阅读Google电子表格的JSON

我想使用PHP从Google Spreadsheet中读取数据,并搜索了一些网页并获得了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查看器返回:

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

+0

@pnuts哦,对不起!这是我的错误!我删除它,先生!如果你知道它,请帮助我! – 2014-10-08 00:07:49

回答

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 
+0

谢谢先生!它工作! – 2014-10-08 00:13:35

+0

@lee_trieu我刚刚使用了你的json字符串响应,你确定你发布了正确的url吗? – Ghost 2014-10-08 00:16:01

+0

@lee_trieu这里是一个工作演示http://codepad.viper-7.com/rQ6RKn – Ghost 2014-10-08 00:16:40

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 

?> 
+0

谢谢你的发帖,先生! – 2014-10-08 00:17:17