Problem in Wikipedia API

徘徊边缘 提交于 2019-12-24 09:57:29

问题


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


回答1:


<?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?




回答2:


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



来源:https://stackoverflow.com/questions/3782553/problem-in-wikipedia-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!