2013-03-09 20 views
1

我想從維基百科獲得用戶輸入公司的摘要段落。如何獲得針對不同公司的維基百科的摘要?

因此,例如,如果用戶輸入Google,我需要顯示Google的摘要段落。

我使用目前的代碼:

// action=parse: get parsed text 
// page=$input 
// format=json: in json format 
// prop=text: send the text content of the article 
// section=0: top content of the page 

$url = 'http://en.wikipedia.org/w/api.php?action=parse&page=$input&format=json&prop=text&section=0'; 
$ch = curl_init($url); 
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt ($ch, CURLOPT_USERAGENT, "TestScript"); // required by wikipedia.org server; use YOUR user agent with YOUR contact information. (otherwise your IP might get blocked) 
$c = curl_exec($ch); 

$json = json_decode($c); 

$content = $json->{'parse'}->{'text'}->{'*'}; // get the main text content of the query (it's parsed HTML) 

// pattern for first match of a paragraph 
$pattern = '#<p>(.*)</p>#Us'; // http://www.phpbuilder.com/board/showthread.php?t=10352690 
if(preg_match($pattern, $content, $matches)) 
{ 
    // print $matches[0]; // content of the first paragraph (including wrapping <p> tag) 
    print strip_tags($matches[1]); // Content of the first paragraph without the HTML tags. 
} 

哪些工作,如果$input = "Zynga"但如果$input = "Google"因爲它返回 「:[4]的參考。」

+3

看來你應該在DOM中尋找一個'

'順便說一下,我認爲你還需要顯示CC許可證和WP作爲源代碼 – 2013-03-09 18:00:11

回答