views:

43

answers:

1

I have problem In wikipedia api I use this php script

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

  print $xmlDoc->saveXML();
?>

& I have this result in browser .... why?

Warning: DOMDocument::load(http://en.wikipedia.org/w/api.php?action=query&amp;prop=revisions&amp;titles=New_York_Yankees&amp;rvprop=content&amp;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&amp;prop=revisions&amp;titles=New_York_Yankees&amp;rvprop=content&amp;format=xml" in D:\Program Files\VertrigoServ\www\wiki\index.php on line 3

A: 
<?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&amp;prop=revisions&amp;titles=New_York_Yankees&amp;rvprop=content&amp;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&amp;prop=revisions&amp;titles=New_York_Yankees&amp;rvprop=content&amp;format=xml");

  print $xmlDoc->saveXML();
?>

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

Wrikken