<?php
// Yahoo! JAPAN Search Services PHP Example Code Ver.1(2005/11/30)
//
require './common.php';
$q=build_query();
// Then send it to the appropriate service
$xml = file_get_contents($service[$_REQUEST['type']].$q);
/*呼び出しクエリーを作成し、APIを呼び出します。
呼び出した結果をそのまま文字列として受け取ります。*/
// Parse the XML and check it for errors
if (!$dom = domxml_open_mem($xml,DOMXML_LOAD_PARSING,$error)) {
echo "XML parse error\n";
foreach ($error as $errorline) {
/* For production use this should obviously be logged to a file instead */
echo $errorline['errormessage']."<br />\n";
echo " Node : " . $errorline['nodename'] . "<br />\n";
echo " Line : " . $errorline['line'] . "<br />\n";
echo " Column : " . $errorline['col'] . "<br />\n";
}
done();
}
/*domxml_open_memを使って、APIの戻り値として受け取ったXMLを
解析し、XMLオブジェクトを作成します。*/
// Now traverse the DOM with this function
// It is basically a generic parser that turns limited XML into a PHP array
// with only a couple of hardcoded tags which are common across all the
// result xml from the web services
function xml_to_result($dom) {
/*XMLの内容から、文字列配列を取得する関数です*/
$root = $dom->document_element();
/*document_element()メソッド*/でルートエレメントを取得
$res['totalResultsAvailable'] = $root->get_attribute('totalResultsAvailable');/*マッチしたデータ数*/
$res['totalResultsReturned'] = $root->get_attribute('totalResultsReturned');/*実際に戻ってきたデータ数*/
$res['firstResultPosition'] = $root->get_attribute('firstResultPosition');/*全検索結果の先頭位置*/
/*get_attribute()メソッドでルートの属性値を取得する*/
$node = $root->first_child();
/*first_child()メソッドで、ルート「ResultSet」の子ノード「Result」にアクセスする*/
$i = 0;
while($node) {
switch($node->tagname) {
case 'Result':/*XMLタグがResult(検索結果)の場合*/
/* Resultタグの子ノード「Title」「Summary」「Url」「ClickUrl」
「ModificationDate」「MimeType」「Cache」にアクセスする*/
$subnode = $node->first_child();
while($subnode) {
$subnodes = $subnode->child_nodes();
if(!empty($subnodes)) foreach($subnodes as $k=>$n) {
if(empty($n->tagname)) $res[$i][$subnode->tagname] = trim($n->get_content());
/*タグ名をキーとする配列に値を格納する*/
else $res[$i][$subnode->tagname][$n->tagname]=trim($n->get_content());
/*Cacheタグは子ノード「URL」を持つので、URLを取得*/
/*get_content()メソッドにてノードの値を取得*/
}
$subnode = $subnode->next_sibling();
/*next_sibling()メソッドで次の兄弟ノードへ*/
}
break;
default:
$res[$node->tagname] = trim($node->get_content());
$i--;
break;
}
$i++;
$node = $node->next_sibling();
}
return $res;
}
$res = xml_to_result($dom); /*上記の実行*/
// Ok, now that we have the results in an easy to use format,
// display them. It's quite ugly because I am using a single
// display loop to display every type.
$first = $res['firstResultPosition'];
$last = $first + $res['totalResultsReturned']-1;
echo "<p>Matched ${res[totalResultsAvailable]}, showing $first to $last</p>\n";
if(!empty($res['ResultSetMapUrl'])) {
echo "<p>Result Set Map: <a href=\"${res[ResultSetMapUrl]}\">${res[ResultSetMapUrl]}</a></p>\n";
}
for($i=0; $i<$res['totalResultsReturned']; $i++) {
foreach($res[$i] as $key=>$value) {
switch($key) {
case 'Thumbnail':/*サムネイル画像をIMGタグで表示*/
echo "<img src=\"${value[Url]}\" height=\"${value[Height]}\" width=\"${value[Width]}\" />\n";
break;
case 'Cache':/*サーバーキャッシュのURLを表示*/
echo "Cache: <a href=\"${value[Url]}\">${value[Url]}</a> [${value[Size]}]<br />\n";
break;
case 'PublishDate':/*最終更新日*/
case 'ModificationDate':
echo "<b>$key:</b> ".strftime('%X %x',$value)."<br />\n";
break;
default:/*タイトル、URLなど*/
if(stristr($key,'url')) echo "<a href=\"$value\">$value</a><br />\n";
else echo "<b>$key:</b> $value<br />";
break;
}
}
echo "<hr />\n";
}
next_prev($res, $first, $last);
done();
?>
|