php - 将 MediaWiki API 与“继续”命令结合使用

标签 php mediawiki mediawiki-api

我需要一些帮助,使用 Mediawiki API 和“继续”或“查询继续”命令从我的 wiki 文章中提取信息。我有大量的 wiki 文章(目前超过 800 篇),我需要使用 api 批量拉取它们,每次 50 篇,然后打印 ou 部分。

我的 API 调用工作正常:

//Stackoverflow 让我在这里使用有效的 URL,这个 api 实际上是我自己的本地主机服务器 http://en.wikipedia.org/w/api.php?action=query&list=allpages&apfrom=a&apto=z&apnamespace=0&format=xml&aplimit=50我正在查询所有页面,因此“apfrom”和“apto”。

我只需要帮助使用 PHP 和 CURL 处理代码,访问 API 并处理 50 个批处理,并使用“继续”访问更多记录,直到结束。到目前为止我的 php 代码是:

  //the CURL commands here work and outputs a data set but only for the first 50 records, so I need to call "continue" to get to the end.  
 //My api url is localhost but I'm forced to use a valid URL by Stackoverflow.com
$url = sprintf('http://en.wikipedia.org/w/api.php?                                                                                            
 action=query&list=allpages&apfrom=a&apto=z&apnamespace=0&format=xml&aplimit=50');
$ch=curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, 'My site');
$res = curl_exec($ch);

$continue = '';

while ( // I don't know what to set here as true to get the while loop going, maybe continue                      = true? maybe set query-continue as true?) 
{
    //Maybe I need something other than $res['query-continue]??
    if (empty($res['query-continue'])) 
    {
        exit;
    }
    else 
    {
        $continue = '&apcontinue='.urlencode($res['query-continue']);
            foreach ($res['query']['allpages'] as $v) 
            {
                echo $v['title'];
            }
    }
}

有人可以更正我上面的 while 循环代码,以便我可以简单地打印循环中每篇 wiki 文章的标题吗?我在网上搜索了很多,但我被困住了!我在 http://www.mediawiki.org/wiki/API:Query 找到了一个 python 循环示例但我必须用 PHP 来做。我不确定我是否调用继续或查询继续。

最佳答案

正如 svick 所说,请使用 client library它为您处理延续性。

MediaWiki 中的查询延续机制已经改变了多次,你不想理解它,甚至更少依赖它。

关于php - 将 MediaWiki API 与“继续”命令结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17247452/

相关文章:

PHP Swiftmailer : Write mail content and other information to DB after every mail sent

namespaces - Mediawiki 自定义命名空间 ID 更改

api - 维基百科 API 返回排序数据而不是随机数据

mediawiki - 如何获取其他语言的维基百科文章的名称

Javascript AJAX 调用范围 - 可能很愚蠢

php - 下拉菜单不起作用

Python MediaWiki 表正则表达式(查找特定格式的字符串,然后提取其中的子字符串)

Javascript仅对某些特定的div元素生效

api - Wikipedia list =搜索REST API : how to retrieve also Url of matching articles

php - 显示关注者和登录用户的帖子