Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extracting data from Wikipedia API

I would like to be able to extract a title and description from Wikipedia using json. So... wikipedia isn't my problem, I'm new to json and would like to know how to use it. Now I know there are hundreds of tutorials, but I've been working for hours and it just doesn't display anything, heres my code:

<?php
  $url="http://en.wikipedia.org/w/api.php?action=query&prop=extracts|info&exintro&titles=google&format=json&explaintext&redirects&inprop=url";

    $json = file_get_contents($url);
    $data = json_decode($json, TRUE);

    $pageid = $data->query->pageids;
    echo $data->query->pages->$pageid->title;
?>

Just so it easier to click:

  • http://en.wikipedia.org/w/api.php?action=query&prop=extracts|info&exintro&titles=google&format=json&explaintext&redirects&inprop=url&indexpageids

I know I've probably just done a tiny thing wrong, but its really bugging me, and the code... I'm used to using xml, and I have pretty much just made the switch, so can you explain it a bit for me and for future visitors, because I'm very confused... Anything you need that I haven't said, just comment it, im sure I can get it, and thanks, in advance!

like image 837
Connor Avatar asked Jun 18 '13 10:06

Connor


2 Answers

$pageid was returning an array with one element. If you only want to get the fist one, you should do this:

$pageid = $data->query->pageids[0];

You were probably getting this warning:

 Array to string conversion 

Full code:

    $url = 'http://en.wikipedia.org/w/api.php?action=query&prop=extracts|info&exintro&titles=google&format=json&explaintext&redirects&inprop=url&indexpageids';

    $json = file_get_contents($url);
    $data = json_decode($json);

    $pageid = $data->query->pageids[0];
    echo $data->query->pages->$pageid->title;
like image 68
Alvaro Avatar answered Oct 31 '22 01:10

Alvaro


I'd do it like this. It supports there being multiple pages in the same call.

$url = "http://en.wikipedia.org/w/api.php?action=query&prop=extracts|info&exintro&titles=google&format=json&explaintext&redirects&inprop=url";

$json = file_get_contents($url);
$data = json_decode($json, TRUE);

$titles = array();
foreach ($data['query']['pages'] as $page) {
    $titles[] = $page['title'];
}
var_dump($titles);
/* var_dump returns
array(1) {
  [0]=>
  string(6) "Google"
}
*/
like image 22
Joel Hinz Avatar answered Oct 31 '22 00:10

Joel Hinz