2014-11-22 71 views
-1

我試圖通過使用API​​訪問Wikipedia的信息框中的各種屬性。我正在使用以下腳本來搜索Google詞語,並希望返回該行業,但是會收到消息未定義的屬性...並注意:嘗試獲取非對象的屬性。我確信我錯過了一些非常簡單的事情,請協助。謝謝。訪問數組屬性時出錯json

<html> 
<head> 

</head> 
<body> 

<h2>Search</h2> 
<form method="post"> 
Search: <input type="text" name="q" /> 
<input type="submit" value="Submit"> 
</form> 

<?php 
if (isset($_POST['q'])) { 
$search = $_POST['q']; 

$url_2 = "http://en.wikipedia.org/w/api.php?                 
action=query&prop=revisions&rvprop=content&format=json&titles=$search&rvsection=0"; 
$res_2 = file_get_contents($url_2); 
$data_2 = json_decode($res_2); 

?> 

<h2>Search results for '<?php echo $search; ?>'</h2> 
<ol> 
<?php echo $data_2->pages->revisions->industry; ?> 

<?php 
} 
?> 

</body> 
</html> 
+0

您應該從相關變量(url,結果等)的'var_dump()'開始。 – jeroen 2014-11-22 23:17:55

回答

0

編輯

看到 http://php.net/manual/en/control-structures.foreach.php 通過你所描述的修訂陣列循環。

當我看着你正在使用訪問該鏈接時,JSON數據在「頁面」對象返回的消息

Formatting of continuation data will be changing soon. To continue using the 
'rawcontinue' parameter. To begin using the new format, pass an empty string 
for 'continue' in the initial query. 

JSON數據沒有「修訂」的對象,我猜有處理你正在使用的api。

+0

謝謝,但我可以看到「修訂」作爲一個數組 - 關於如何從它獲得數據的任何建議?謝謝。 – Oroku 2014-11-23 00:41:29

+0

如果修訂版本是一個數組,那麼在PHP中,您將執行...->修訂版本[//插入索引//],索引可以是數字0或像'revision'這樣的密鑰字符串,取決於它們如何獲取數據舉辦。你可以循環訪問數組http://php.net/manual/en/control-structures.foreach.php – user2782001 2014-11-23 04:22:12