Google AJAX APIを使ってwikipediaの記事検索をやってみた
wikipedia内の検索機能使いにくいです><
で、考えたのがGoogleでドメイン指定検索を使って
wikipedia内を検索して記事の一覧を取得しよう!という試みです。
$word = $_GET['word']; $wikidatas = googleSearch($word); print_r($wikidatas); function googleSearch($keyword) { require_once "lib/JSON.php"; $json = new Services_JSON; $word = mb_convert_encoding($keyword."site:ja.wikipedia.org", "utf-8", "shift-jis, jis, utf-8"); $word = urlencode($word); $url = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0"; $url .= "&rsz=large"; $url .= "&hl=ja"; $url .= "&q={$word}"; $body = file_get_contents($url); $result = $json->decode($body); return $result->responseData; }
JSON形式で返ってくるので、decodeする必要があります。
PHP5.2環境ならJSONのライブラリ使わなくてもjson_decodeすればOKです。
簡単でした!
ちなみにrsz=large使っても8件までしか取得できないです。
指定ないと4件まで。以前やっていた画像取得と同じですね。
http://d.hatena.ne.jp/Arko/20090424/1240579432