Curtis
Curtis

Reputation: 2545

How to get results from the Wikipedia API with PHP?

I'm probably not supposed to use file_get_contents() What should I use? I'd like to keep it simple.

Warning: file_get_contents(http://en.wikipedia.org/w/api.php?action=query&titles=Your_Highness&prop=revisions&rvprop=content&rvsection=0): failed to open stream: HTTP request failed! HTTP/1.0 403 Forbidden

Upvotes: 2

Views: 19774

Answers (4)

jon
jon

Reputation: 6246

file_get_contents Should work.

file_get_contents('http://en.wikipedia.org/w/api.php?action=query&prop=revisions&titles=New_York_Yankees&rvprop=timestamp|user|comment|content')

This was previously discussed on stackoverflow here

Also, some nice looking code samples here

Upvotes: 1

favo
favo

Reputation: 5458

The error message you are really receiving is

Scripts should use an informative User-Agent string with contact information, or they may be IP-blocked without notice.

This means that you should provide additional details about yourself when using the API. Your usage of file_get_contents does send the required User-Agent.

Here is a working example in curl that identifies itself as a Test for this question:

<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://en.wikipedia.org/w/api.php?action=query&titles=Your_Highness&prop=revisions&rvprop=content&rvsection=0&format=xml");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_USERAGENT, "Testing for http://stackoverflow.com/questions/8956331/how-to-get-results-from-the-wikipedia-api-with-php");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);

echo $result;
?>

Upvotes: 1

DaveRandom
DaveRandom

Reputation: 88697

The problem you are running into here is related to the MW API's User-Agent policy - you must supply a User-Agent header, and that header must supply some means of contacting you.

You can do this with file_get_contents() with a stream context:

$opts = array('http' =>
  array(
    'user_agent' => 'MyBot/1.0 (http://www.mysite.com/)'
  )
);
$context = stream_context_create($opts);

$url = 'http://en.wikipedia.org/w/api.php?action=query&titles=Your_Highness&prop=revisions&rvprop=content&rvsection=0';
var_dump(file_get_contents($url, FALSE, $context));

Having said that, it might be considered more "standard" to use cURL, and this will certainly give you more control:

$url = 'http://en.wikipedia.org/w/api.php?action=query&titles=Your_Highness&prop=revisions&rvprop=content&rvsection=0';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_USERAGENT, 'MyBot/1.0 (http://www.mysite.com/)');

$result = curl_exec($ch);

if (!$result) {
  exit('cURL Error: '.curl_error($ch));
}

var_dump($result);

Upvotes: 13

whizzzkid
whizzzkid

Reputation: 1295

They themselves say in their API documentation:

Use any programming language to make an HTTP GET request for that URL

You need to get the URL right, thefollowing worksfor me : http://en.wikipedia.org/w/api.php?format=json&action=query&titles=Main%20Page&prop=revisions&rvprop=content

you are not specifying the output format as far as I can notice right now!

Upvotes: 0

Related Questions