2014-10-07 100 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