2013-06-22 3 views
1

약간의 도움이 필요합니다 위키 기사에서 정보를 가져 오려면 Mediawiki API를 "계속하기"또는 "query-continue"명령과 함께 사용하십시오. 나는 많은 wiki 기사 (현재 800 개 이상)를 가지고 있으며 api를 사용하여 50 개 배치로 가져온 다음 ou 섹션을 인쇄해야합니다.계속 명령으로 MediaWiki API 사용하기

내 API 호출이 제대로 작동 :

// 유래 나 여기 유효한 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"입니다.

API에 액세스하여 50 개의 배치를 처리하고 끝에 도달 할 때까지 더 많은 레코드에 액세스하기 위해 "계속"을 사용하여 PHP 및 CURL로 코드를 처리하는 데 도움이 필요합니다. 지금까지 내 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 루프 코드를 수정할 수 있습니까? 나는 온라인 검색을 많이했으나 막혔다 !! http://www.mediawiki.org/wiki/API:Query에서 파이썬 루프 예제를 발견했지만 PHP로해야합니다. 계속 또는 쿼리를 계속 호출하면 확실하지 않습니다.

+0

모든 페이지를 조회하려면 'apfrom'과'apto '를 설정하지 마십시오. 모든 페이지 이름이 문자로 시작해야하는 것은 아닙니다. – svick

+0

또한 [API 라이브러리] (http://en.wikipedia.org/wiki/Wikipedia:Creating_a_bot#PHP)를 사용하지 않는 이유는 무엇입니까? – svick

+0

botclasses.php 라이브러리를 사용하고 있지만 필요한 쿼리 기능이 없습니다. 다른 botclass 라이브러리 중 일부는 저를 위해 작동하지 않으므로 계속 사용하여 자체 함수를 작성해야합니다. – Peter

답변

1

svick이 말한대로 계속을 처리하는 client library을 사용하십시오.

MediaWiki에서 쿼리 계속 메커니즘이 여러 번 변경되었지만이를 이해하고 싶지 않거나 더 적게 의존하고 싶지는 않습니다.