Example Code Usage

February 6th, 2007 by jay

Example Usage
Here are 3 examples of using phpYahoo to search for the Web, Videos and Images.


Invoking the phpYahoo Class

PHP:

  1. include_once('api/phpYahoo.php');
  2.  
  3. $y = new phpYahoo('jrrxraqsrfgviny');
  4. $y->enableCache('db');
  5.  
  6. $searchtext= $GET['searchtext'];

Yahoo! Web Search

PHP:

  1. $params = array("query"=>$keyword,"region"=>"us","type"=>"any","results"=>"20","start"=>$start);
  2. $results_web = $y->searchWeb($params); //search text and number of results MAX:50
  3.  
  4. foreach($results_web as $result) {
  5.  
  6.     $html_web .= "<p style='background:".$bgcolor.";'>\n";
  7.  
  8.     //display a link to the search item result
  9.  
  10.     $html_web .= "<a href='".$result['clickUrl']."' title='".$result['displayUrl']."'>".$result['title']."</a>";
  11. $html_web .= "<br>";
  12.  
  13.     //display the summary
  14.  
  15.     $html_web .= $result['summary']; $html_web .= "<br>";
  16. $html_web .= "</p>\n"; $count++;
  17.  
  18. }
  19. echo $html_web;


Yahoo! Video Search

PHP:

  1. $params = array("query"=>$keyword,"region"=>"us","type"=>"any","results"=>"20","start"=>$start);
  2. $results_web = $y->searchVideos($params);
  3.  
  4. foreach($results_video as $result) {
  5.  
  6.     $html_video .= "<p>\n";
  7. $html_video .= "<a href='".$result['clickUrl']."' title='".$result['displayUrl']."'>".$result['title']."</a>";
  8. $html_video .= "<br>";
  9. $html_video .= $result['summary']; $html_video .= "<br>";
  10.  
  11.     //display a thumbnail of the video with a link
  12.  
  13.     $html_video .= "<a href='".$result['url']."' title='".$result['title']."'><img src='".$result['thumbnail']."'></a>";
  14.  $html_video .= "<br>"; $html_video .= "</p>\n";
  15.  
  16. }
  17. echo $html_video ;

Yahoo! Image Search

PHP:

  1. $params = array("query"=>$keyword,"region"=>"us","type"=>"any","results"=>"20","start"=>$start);
  2. $results_images = $y->searchImages($params);
  3.  
  4. OR
  5.  
  6. $results_images = $y->searchImages($searchtext);
  7. $html_images .= "<p>\n"; $html_images .= "<table><tr valign=top>\n";
  8. $i=0;
  9. foreach($results_images as $result) {
  10.  
  11.     $html_images .= "<td width='23%' align='center'>\n";
  12.  $html_images .= "<a href='".$result['url']."' title='".$result['title']."'><img src='".$result['thumbnail']."' border='0'></a>"; $html_images .= "<br>";
  13.     $html_images .= "</td>\n"; $i++; if($i==3) {
  14.         $html_images .= "</tr><tr valign=top>"; $i=0;
  15.  
  16.     }
  17.  
  18. }
  19. $html_images .= "</table></p>\n";
  20.  
  21. echo $html_images;

Posted in Documentation |

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.