上記のサンプルに手を加えて、「はてな」検索を実行してみます。
<html>
<head>
<title>Open Searchで「はてな」検索</title>
</head>
<body>
<h1>はてな検索結果</h1>
<hr>
<?
require_once 'Services/OpenSearch.php';
$url = 'http://search.hatena.ne.jp/osxml';
/*「はてな」用のSiteInfoファイルを指定します*/
$os = new Services_OpenSearch ($url);
$items = $os->search ('PHP');/*キーワードは同じく'PHP'*/
foreach ($items as $item) {
$link = mb_convert_encoding ($item["link"], "SJIS", "UTF-8");
$title = mb_convert_encoding ($item["title"], "SJIS", "UTF-8");
$description = mb_convert_encoding ($item["description"], "SJIS", "UTF-8");
/*UTF-8をShift-JISに変換しています*/
echo "<a href=\"" . $link . "\">" . $title . "</a><br>"
. $description . "<br>";
}
echo "<hr>計" . count ($items) . "件";
?>
</body>
</html>
コードはこれだけです。非常に簡単ですね。
実行すると、「はてな」で「PHP」を検索のようになります。
|