以下代码从 Wikipedia 页面中获取第一段。
<?
// action=parse: get parsed text
// page=Baseball: from the page Baseball
// format=json: in json format
// prop=text: send the text content of the article
// section=0: top content of the page
$find = $_GET['find'];
$url = 'http://en.wikipedia.org/w/api.php?action=parse&page=baseball&format=json&prop=text§ion=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>#s'; // http://www.phpbuilder.com/board/showthread.php?t=10352690
if(preg_match_all($pattern, $content, $matches))
{
// print $matches[0]; // content of the first paragraph (including wrapping <p> tag)
echo "Wikipedia:<br>";
print strip_tags(implode("\n\n",$matches[1])); // Content of the first paragraph without the HTML tags.
}
?>
问题是有时我想让标题成为 PHP 中的变量,以便我可以“搜索”信息,但我的查询并不总是合法的 Wikipedia 页面。
例如,当上面的代码搜索棒球时,有一个棒球页面。但是当我搜索“普通话”时,它显示:
Mandarin may refer to any of the following:
但它没有显示任何选项。
我的问题是,有没有办法检查页面是否存在,如果不存在,从维基百科获取可能存在的选项列表,然后选择要显示的第一页?