Examples Updated

February 21st, 2007 by jay

I have added example code to the web search, video search and the image search.

If you look at the code for each example you'll see that they all share a common structure:

Include the phpYahoo.php file

PHP:

  1. include_once('api/phpYahoo.php');

Instantiate a new phpYahoo class

PHP:

  1. $y = new phpYahoo('YAHOOAPPID');

Get the search keyword(s) and create an array of parameters

PHP:

  1. $keyword = $_GET['keyword'];
  2. $params = array("query"=>$keyword,"region"=>"us","type"=>"any","results"=>"20");

Call the webSearch function and get the results

PHP:

  1. $results = $y->searchWeb($params);

Loop through the results and assign it to a variable as HTML

PHP:

  1. foreach($results as $result) {
  2. $html .= "<h3><a href='".$result['clickUrl']."' title='".$result['displayUrl']."'>".$result['title']."</a></h3>";
  3. $html .= $result['summary'];
  4. $html .= "<br>";                   
  5. }

Add a FORM to search from

HTML:

  1. <form method="get" class="mainsearch" action="websearch.php">      
  2. <input name="keyword" id='keyword' class="maintextbox" type="text" value="<?php echo $_GET['keyword']; ?>" />
  3. <input name="search" class="mainsearchbutton" value="Search" type="submit" />
  4. </form>

Display the results

PHP:

  1. <?php echo $html;?>

Posted in phpYahoo, Documentation, Source Code, Example |

Leave a Comment

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