使用MediaWiki API和Continue命令

时间:2013-06-22 04:59:45

标签: php mediawiki mediawiki-api

我需要一些帮助使用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文章中做一个简单的打印标题吗?我在线搜索了很多,但我被卡住了!我在http://www.mediawiki.org/wiki/API:Query找到了一个python循环示例,但我必须在PHP中完成。我不确定我是打电话继续还是继续查询。

0 个答案:

没有答案
相关问题