Error accessing array properties json

74 Views Asked by At

I am trying to access various properties from Wikipedia's Infobox through using the API. I am using the following script for a search term Google for example and would like to return the industry however am getting the messages Undefined property... and Notice: Trying to get property of non-object. I'm sure I'm missing something really simple please assist. Thank you.

<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>
1

There are 1 best solutions below

2
On

Edit

see http://php.net/manual/en/control-structures.foreach.php for looping through the revision array you are describing.

When I look at the link you are using to access, the JSON data returns the message

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.

the JSON data has no "revisions" object under the "pages" object, I'm guessing having to do with the api you are using.