user1141796
user1141796

Reputation: 324

Wikipedia PHP - handle results

I use the MediaWiki api to get Wikipedia a PHP serialized arrray of information about a specific country.

http://en.wikipedia.org/w/api.php?action=query&titles=Belgium&prop=revisions&rvprop=content&rvsection=0&format=php

My question

How do I get a specific field out of this array? Like f.e.:

I'm not familiar with this output... Thanks!

Upvotes: 1

Views: 137

Answers (3)

hek2mgl
hek2mgl

Reputation: 157947

The api is not meant to access properties like capital for belgium or something like that. You will just get the wikipage as wiki source code together with some extra information like page title. A wiki page can for example contain information about a programming language, a flower, a car or a country in your example, it is just markdown no special data fields.

The response format is serialized php data. Use unserialize to parse it into an array but don't expect to get structured information about belgium:

$response = file_get_contents('http://en.wikipedia.org/w/api.php?action=query&titles=Belgium&prop=revisions&rvprop=content&rvsection=0&format=php');
$data = unserialize($response);
var_dump($data);

Upvotes: 1

Nil'z
Nil'z

Reputation: 7475

Use this function unserialize():

$array = file_get_contents('http://en.wikipedia.org/w/api.php?action=query&titles=Belgium&prop=revisions&rvprop=content&rvsection=0&format=json');
$array = unserialize( $array );
print_r( $array ); 

Upvotes: 0

anon
anon

Reputation:

It looks like serialized PHP and you can unserialize() it as others have answered. I'd suggest using the JSON format instead:

http://en.wikipedia.org/w/api.php?action=query&titles=Belgium&prop=revisions&rvprop=content&rvsection=0&format=json

You can then use json_decode() to get parse the JSON response and transform it into an associative array:

$json = file_get_contents($file);
$decoded = json_decode($json, TRUE);

foreach ($decoded as $key => $value) {
    // get the details you need
}

Upvotes: 0

Related Questions