2

I have tried many different variations here - how can I get the content summary from this: http://en.wikipedia.org/w/api.php?format=json&action=query&titles=stack_overflow&prop=revisions&rvprop=content&rvsection=0c

Here's what I have so far:

$wiki_url = "http://en.wikipedia.org/w/api.php?format=json&action=query&titles=stack_overflow&prop=revisions&rvprop=content&rvsection=0c.json";
$json = file_get_contents($wiki_url);
$data = json_decode($json, TRUE);

I know I need to do something like echo $data[0]->query but am unable to get it to do anything.

Sam
  • 6,616
  • 8
  • 35
  • 64
  • 1
    Since you set the second argument of `json_decode` to true, you are working with an array only. You may use `$data['query'][...]` freely. – Dave Chen Nov 30 '13 at 03:54

1 Answers1

2
$wiki_url = "http://en.wikipedia.org/w/api.php?format=json&action=query&titles=stack_overflow&prop=revisions&rvprop=content&rvsection=0c.json";
$json = file_get_contents($wiki_url);
$data = json_decode($json, TRUE);

foreach ($data['query']['pages'] as $page) {
    print_r($page['revisions'][0]['*']);
}
Ilmari Karonen
  • 49,047
  • 9
  • 93
  • 153
bagonyi
  • 3,258
  • 2
  • 22
  • 20
  • Ps. Depending on what you want to do with the content, you might also want to [consider](http://stackoverflow.com/a/12337968/411022) using [prop=extracts](http://en.wikipedia.org/w/api.php?format=jsonfm&action=query&titles=stack_overflow&prop=extracts&exchars=1000) instead. – Ilmari Karonen Dec 01 '13 at 16:16