【问题标题】:php and MediaWikiphp 和 MediaWiki
【发布时间】:2011-04-12 15:25:11
【问题描述】:

我希望使用他们的 api 检索 Wikipedia 页面的 XML。我使用的网址如下:http://en.wikipedia.org/w/api.php?action=query&prop=revisions&rvprop=content&format=xml&redirects&titles=dog

我见过this,但没有帮助。无论我做什么,我实际上都没有得到任何返回到 $c 的东西,我不知道为什么。我可以用纯文本文件做file_get_contents,它工作得很好。其他人可以验证这是否有效吗?

<?php
$url = 'http://en.wikipedia.org/w/api.php?action=query&prop=revisions&rvprop=content&format=xml&redirects&titles=Main%20Page';
$c = file_get_contents($url);
echo $c;
?>

编辑我也尝试了该页面上可用的 cURL,但也不起作用:

$url = 'http://en.wikipedia.org/w/api.php?action=query&prop=revisions&rvprop=content&format=xml&redirects&titles=Main%20Page';
$ch = curl_init($url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
$c = curl_exec($ch);
echo $c;

【问题讨论】:

  • 您的托管公司可能在 file_get_contents 上禁用了 URL,您是否尝试过 curl?
  • Warning: file_get_contents(http://en.wikipedia.org/w/api.php?action=query&amp;prop=revisions&amp;rvprop=content&amp;format=xml&amp;redirects&amp;titles=Main%20Page) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.0 403 Forbidden in [file]
  • 我也尝试过 curl,但我也无法开始工作。我已经把它贴在上面供参考了。

标签: php mediawiki


【解决方案1】:

wikipedia 要求您通过执行以下操作指定描述性用户代理:

<?php
$url = 'http://en.wikipedia.org/w/api.php?action=query&prop=revisions&rvprop=content&format=xml&redirects&titles=Main%20Page';
$ch = curl_init($url);
curl_setopt ($ch, CURLOPT_USERAGENT, "MyCoolTool (+http://example.com/MyCoolToolPage/)");
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
$c = curl_exec($ch);
echo $c;
?>

您应该使用描述您网站的用户代理字符串,并且您不应该欺骗网络浏览器的用户代理,否则您可能会因为显得可疑而被阻止(来源:WikiMedia User-Agent policy

【讨论】:

  • 不要使用浏览器用户代理,否则您的 IP 地址可能会被系统管理员禁止。使用可以识别您的程序并包含您的电子邮件或网站地址的东西。详情请见Wikimedia's User-Agent policy
  • @Anomie,谢谢。我已经更新了我的答案以考虑到这一点。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-02-28
  • 1970-01-01
  • 2013-11-11
  • 1970-01-01
  • 2018-11-09
  • 2012-12-15
  • 2013-01-06
相关资源
最近更新 更多