2009-05-11 50 views
2

嗨,大家好,我希望從維基百科獲取我的數據庫中的條目信息,例如某些體育場和國家信息。我正在使用Zend框架,以及我將如何能夠處理返回多個模糊條目或類似的查詢..我想所有的幫助,我可以在這裏...我如何從維基百科獲取信息到我的應用程序中

回答

2

做一個簡單的HTTP request到文章你正在尋找導入。 Here'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

+0

定義哇我沒不知道你可以這樣做 - 我已經下載了數據庫,並開始一個新的線程上的導入它的細心任務 - 感謝提示男子:) – Ali 2009-05-16 09:44:10

11

Wikipedia基於MediaWiki,提供應用程序可編程接口(API)。

您可以在維基百科上查了鏈接到MediaWiki API - http://en.wikipedia.org/w/api.php

的文檔鏈接到MediaWiki API - http://www.mediawiki.org/wiki/API

+0

這很酷,但有沒有任何tuto里亞爾或入門代碼開始。基本上我沒有文章的確切名稱只是我的數據庫中的一個條目,我想知道是否可以從維基百科獲得相應的文章。 – Ali 2009-05-11 11:47:23

2

This博客有一個非常好的代碼得到維基

<?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/>"; 

?> 
+0

看起來很有趣 - 感謝提示! – Ali 2014-02-12 07:14:28

相關問題