如何从维基百科中获取信息到我的应用程序中

时间:2009-05-11 10:24:59

标签: php zend-framework wikipedia

大家好,我希望从wikipedia获取我在数据库中的条目信息,例如一些体育场和国家信息。我正在使用Zend Framework以及如何处理返回多个模糊条目的查询等等。我希望能得到所有帮助......

3 个答案:

答案 0 :(得分:11)

Wikipedia基于MediaWiki,提供应用程序可编程接口(API)。

您可以在维基百科上查看MediaWiki API - http://en.wikipedia.org/w/api.php

MediaWiki API的文档 - http://www.mediawiki.org/wiki/API

答案 1 :(得分:2)

对您要导入的文章执行简单的HTTP requestHere's a good library可能有助于解析HTML,尽管有很多解决方案,including using the standard DOM model which is provided by php

<?php
require_once "HTTP/Request.php";

$req =& new HTTP_Request("http://www.yahoo.com/");
if (!PEAR::isError($req->sendRequest())) {
    echo $req->getResponseBody();
}
?> 

请注意,如果您的流量水平过高,您将被锁定在网站之外。 (如果您需要大量文章,download the database

答案 2 :(得分:2)

This博客有一个很好的代码,可以从wiki获得定义

<?php
//FUNCTION THAT :PARAMETER - KEYWORD , AND RETURNS WIKI DEFINITION (IN ARRAY FORMAT)
function wikidefinition($s) {
//ENGLISH WIKI
    $url = "http://en.wikipedia.org/w/api.php?action=opensearch&search=".urlencode($s)."&format=xml&limit=1";
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_HTTPGET, TRUE);
    curl_setopt($ch, CURLOPT_POST, FALSE);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_NOBODY, FALSE);
    curl_setopt($ch, CURLOPT_VERBOSE, FALSE);
    curl_setopt($ch, CURLOPT_REFERER, "");
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
    curl_setopt($ch, CURLOPT_MAXREDIRS, 4);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.1; he; rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8");

    $page = curl_exec($ch);
    $xml = simplexml_load_string($page);
    if((string)$xml->Section->Item->Description) {
        return array((string)$xml->Section->Item->Text, 
                     (string)$xml->Section->Item->Description, 
                     (string)$xml->Section->Item->Url);
    } else {
        return "";
    }
}
//END OF FUNCTION WIKIDEFINITIONS


//USE OF FUNCTION
$data = wikidefinition('Bangladesh') ;
//var_dump( wikidefinition('bangladesh') ) ; //displays the array content
echo "Word:"       . $data[0] . "<br/>";
echo "Definition:" . $data[1]  . "<br/>";
echo "Link:"       . $data[2] . "<br/>";

?>