【问题标题】:Using of Wikipedia API with Rest Clients [duplicate]将 Wikipedia API 与 Rest 客户端一起使用 [重复]
【发布时间】:2013-03-02 16:42:31
【问题描述】:

我正在尝试使用 MediaWiki 获取维基百科页面(来自特定类别)。为此,我正在关注this 教程清单 3. 列出类别中的页面。我的问题是:如何在不使用 Zend 框架的情况下获取维基百科页面?有没有不需要安装的基于php的Rest Clients?因为 Zend 需要先安装他们的包和一些配置......而我不想做所有这些事情。

经过谷歌搜索和一些调查,我发现了一个名为 cURL 的工具,将 cURL 与 PHP 一起使用也可以构建一个 rest 服务。我在实现休息服务方面真的很新,但已经尝试在 php 中实现一些东西:

<?php
    header('Content-type: application/xml; charset=utf-8');

    function curl($url) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $data = curl_exec($ch);
        curl_close($ch);
        return $data;
    }
    $wiki = "http://de.wikipedia.org/w/api.php?action=query&list=allcategories&acprop=size&acprefix=haut&format=xml";
    $result = curl($wiki);
    var_dump($result);
?>

但在结果中出现错误。有人可以帮忙吗?

更新:

This page contains the following errors:
error on line 1 at column 1: Document is empty
Below is a rendering of the page up to the first error.

【问题讨论】:

  • 如果您告诉我们错误是什么可能会有所帮助。
  • @IlmariKaronen 请查看问题的update 部分。

标签: php rest mediawiki-api


【解决方案1】:

很抱歉这么久才回复,但迟到总比没有好...

当我在命令行上运行你的代码时,我得到的输出是:

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

因此,问题似乎在于您通过不告诉 cURL 发送自定义 User-Agent 标头而遇到Wikimedia bot User-Agent policy。要解决此问题,请遵循该页面底部给出的建议,并将以下行添加到您的脚本中(与其他 curl_setopt() 调用一起):

$agent = 'ProgramName/1.0 (http://example.com/program; your_email@example.com)';
curl_setopt($ch, CURLOPT_USERAGENT, $agent);

附言。您可能也不想设置 application/xml 内容类型,除非您确定该内容实际上是有效的 XML。特别是,var_dump() 的输出将不是是有效的 XML,即使输入是。

对于测试和开发,我建议从命令行运行 PHP 或使用 text/plain 内容类型。或者,如果您愿意,可以使用 text/html 并使用 htmlspecialchars() 对您的输出进行编码。


附言。将此作为社区 wiki 答案,因为我意识到这个问题已经是 asked and answered before

【讨论】:

    猜你喜欢
    • 2014-01-14
    • 2017-12-15
    • 2015-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-25
    • 1970-01-01
    相关资源
    最近更新 更多