1

I have problem using the Wikipedia API. I use this PHP script,

<?php
  $xmlDoc = new DOMDocument();
  $xmlDoc->load("http://en.wikipedia.org/w/api.php?action=query&prop=revisions&titles=New_York_Yankees&rvprop=content&format=xml");

  print $xmlDoc->saveXML();
?>

and I have the following result in the browser. Why?

Warning: DOMDocument::load(http://en.wikipedia.org/w/api.php?action=query&prop=revisions&titles=New_York_Yankees&rvprop=content&format=xml) [domdocument.load]: failed to open stream: HTTP request failed! HTTP/1.0 403 Forbidden in D:\Program Files\VertrigoServ\www\wiki\index.php on line 3

Warning: DOMDocument::load() [domdocument.load]: I/O warning : failed to load external entity "http://en.wikipedia.org/w/api.php?action=query&prop=revisions&titles=New_York_Yankees&rvprop=content&format=xml" in D:\Program Files\VertrigoServ\www\wiki\index.php on line 3

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
Tyranitar
  • 37
  • 1
  • 6

2 Answers2

2

For MediaWiki's User Agent policy: http://meta.wikimedia.org/wiki/User-Agent_policy

nl11087
  • 21
  • 1
2
<?php
  $vars = array(
    'http' => array(
      'user_agent' =>'whatever'));
  $context = stream_context_create($vars);
  libxml_set_streams_context($context);
  $xmlDoc = new DOMDocument();
  $xmlDoc->load("http://en.wikipedia.org/w/api.php?action=query&prop=revisions&titles=New_York_Yankees&rvprop=content&format=xml");

  print $xmlDoc->saveXML();
?>

Don't ask my why a user-agent is required, but I see more & more the same questions here on SO, which all can be fixed by supplying a User-Agent.


edit: The following would also work (it does here):

<?php
  ini_set('user_agent','whatever');
  $xmlDoc = new DOMDocument();
  $xmlDoc->load("http://en.wikipedia.org/w/api.php?action=query&prop=revisions&titles=New_York_Yankees&rvprop=content&format=xml");

  print $xmlDoc->saveXML();
?>

Perhaps a default setting in PHP for this user_agent has been changed?

Wrikken
  • 66,931
  • 8
  • 91
  • 133