Reputation: 324
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
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
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
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