PHPのお勉強



Services_Yahoo_Search(PEAR)を利用@PHP

Services_Yahoo_Searchを利用します。
pear install Services_Yahooでインストールします。

Services_Yahoo_Searchを生成します。
検索のタイプは、サイト検索です。
searchForでクエリを設定して、検索を行います。

getTotalResultsReturnedで検索結果の件数が取得できます。
デフォルトでは、取得する検索結果は10となります。

for eachで検索結果をループして、
表示します。


<?php
require_once("Services/Yahoo/Search.php");

try {
    //Services_Yahoo_Searchを生成
    //検索のタイプは、「web」とします。
    $yahooSearch = Services_Yahoo_Search::factory("web");
    
    //クエリを設定して、検索
    $res = $yahooSearch->searchFor("PHP");
    
    //検索結果の件数
    print $res->getTotalResultsReturned();
    print "<BR>";
    
    //検索結果の表示
    foreach ($res as $item) {
        print "サイト名" .$item['Title'] ."、URL:" .$item["Url"] ."<BR>";
    }
    
} catch (Services_Yahoo_Exception $ex) {
    print $ex->getMessage() ."<BR>";

    foreach ($ex->getErrors() as $err) {
        print $err . "<BR>";
    }
}
?>




Copyright (C) PHPのお勉強. All Rights Reserved.