1

HTML

<html>
<head>
</head>
<form action="check2.php" method="POST">
    Search <input type="text" name="search" />
    <input type="submit" name="searchbtn" value="search" />
</form>
</body>
</html>

PHP

<?php
    $string = $_POST['search'];
    $convert = rawurlencode($string);
    $url = 'http://en.wikipedia.org/w/api.php?action=query&prop=revisions&rvprop=content&format=xml&redirects&titles='.$convert;
    if($_POST['search'] != '' || $_POST['search'] != null) {
        $ch = curl_init($url);
        curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
        $c = curl_exec($ch);
        echo $c;
    } else {

    }
?>

如果我搜索(例如)“New York”,它会返回 XML,但如果我搜索“Singapore Zoo”,它什么也不返回

4

1 回答 1

1

首先,您应该设置一个 User-Agent。其次,这不是搜索,而是导出特定标题的页面;您正在寻找action=opensearch

https://en.wikipedia.org/w/api.php?action=opensearch&search=api&limit=1&namespace=0&search=Singapore_Zoo&utf8=true给出:

["Singapore_Zoo",["Singapore Zoo"],["The Singapore Zoo (Chinese: 新加坡动物园; pinyin: Xīnjiāpō Dòngwùyuán; Malay: 'Taman Haiwan Singapura'; Tamil: சிங்கப்பூர் விலங்குக் காட்சிச்சாலை), formerly known as the Singapore Zoological Gardens and commonly known locally as the Mandai Zoo, occupies 28 hectares (69 acres) on the margins of Upper Seletar Reservoir within Singapore's heavily forested central catchment area."],["https://en.wikipedia.org/wiki/Singapore_Zoo"]]
于 2015-04-27T23:10:59.940 回答