2013-03-14 25 views
1

我正在尝试使用MediaWiki获取维基百科页面(来自特定类别)。为此,我正在关注this教程清单3.列出类别中的页面。我的问题是:如何在不使用Zend Framework的情况下获取Wikipedia页面?有没有基于PHP的休息客户端,而无需安装?因为Zend需要首先安装他们的软件包,并且需要一些配置......我不想完成所有这些工作。将维基百科API与其他客户端一起使用

谷歌搜索和一些调查后,我发现了一个名为cURL的工具,使用PHP的cURL也可以建立一个休息服务。我在执行休息服务真的很新,但已经试图在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); 
?> 

,但得到的结果的误差。任何人都可以提供帮助吗?

UPDATE:

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. 
+0

如果你告诉我们错误是什么,它可能会有所帮助。 – 2013-03-14 19:53:12

+0

@IlmariKaronen请看看问题的'update'部分。 – Dozent 2013-03-14 20:48:21

回答

0

对不起,这么长时间回答,但迟到总比不到好...

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

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

如此看来,问题是你撞到Wikimedia bot User-Agent policy从没有告诉卷曲发送自定义User-Agent头。为了解决这个问题,请在该页面的底部提供的意见,并添加线,如以下到脚本(旁边的其他curl_setopt()话费):

$agent = 'ProgramName/1.0 (http://example.com/program; [email protected])'; 
curl_setopt($ch, CURLOPT_USERAGENT, $agent); 

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

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


Ps。这是一个社区维基答案,因为我意识到这个问题已经是asked and answered before