首页 文章

使用MediaWiki API和Continue命令

提问于
浏览
1

我需要一些帮助使用Mediawiki API和“Continue”或“query-continue”命令从我的wiki文章中提取信息 . 我有大量的wiki文章(目前超过800篇),我需要使用api将它们分批拉出50,然后打印出你的部分 .

我的API调用正常:

// Stackoverflow让我在这里使用一个有效的URL,这个api实际上是我自己的localhost服务器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文章中做一个简单的打印 Headers 吗?我've done a lot of searching online but I'被卡住!!我在http://www.mediawiki.org/wiki/API:Query找到了一个python循环示例,但我必须在PHP中完成 . 而且我不确定我是否呼叫继续或查询 - 继续 .

1 回答

  • 1

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

    查询延续机制在MediaWiki中已经多次更改,您不想理解它,甚至更少依赖它 .

相关问题