1

I am having problem with the yahoo search API, sometimes it works and sometimes don't why I am getting problem with that

I am using this URL

http://api.search.yahoo.com/WebSearchService/rss/webSearch.xml?appid=yahoosearchwebrss&query=originurlextension%3Apdf+$search&adult_ok=1&start=$start

The code is given below:

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">   
<? $search = $_GET["search"]; 
$replace = " "; $with = "+"; 
$search = str_replace($replace, $with, $search);
if ($rs =
    $rss->get("http://api.search.yahoo.com/WebSearchService/rss/webSearch.xml?appid=yahoosearchwebrss&query=originurlextension%3Apdf+$search&adult_ok=1&start=$start")
    )
    {   }   
    // Go through the list powered by the search engine listed and get
    // the data from each <item>
    $colorCount="0";
    foreach($rs['items'] as $item)      {       // Get the title of result     
       $title = $item['title'];     // Get the description of the result
       $description = $item['description'];     // Get the link eg amazon.com 
       $urllink = $item['guid'];   
       if($colorCount%2==0) { 
         $color = ROW1_COLOR; 
       } else { 
          $color = ROW2_COLOR; 
       }   
       include "resulttemplate.php"; $colorCount++; 
       echo "\n";  
    }  
 ?>

Sometimes it gives results and sometimes don't. I get this error usually

Warning: Invalid argument supplied for foreach() in /home4/thesisth/public_html/pdfsearchmachine/classes/rss.php on line 14

Can anyone help..

skaffman
  • 398,947
  • 96
  • 818
  • 769
Amit
  • 11
  • 1
  • I hope I have improved the readability of this code by standardizing the formatting. @Amit , you can use 3 more tags, you might get more help if you include the programming language you are using here. Good luck! – shellter Apr 22 '11 at 17:29

1 Answers1

0

The error Warning: Invalid argument supplied for foreach() in /home4/thesisth/public_html/pdfsearchmachine/classes/rss.php on line 14 means the foreach construct did not receive an iterable (usually an array). Which in your case would mean the $rs['items'] is empty... maybe the search returned no results?

I would recommended adding some checks to the results of $rss->get("...") first, and also having an action for when the request fails or returns no results:

<?php
$search = isset($_GET["search"]) ? $_GET["search"] : "default search term";
$start = "something here"; // This was left out of your original code
$colorCount = "0";
$replace = " ";
$with = "+"; 
$search = str_replace($replace, $with, $search);
$rs = $rss->get("http://api.search.yahoo.com/WebSearchService/rss/webSearch.xml?appid=yahoosearchwebrss&query=originurlextension%3Apdf+$search&adult_ok=1&start=$start");

if (isset($rs) && isset($rs['items'])) {
    foreach ($rs['items'] as $item) {
        $title       = $item['title'];       // Get the title of the result
        $description = $item['description']; // Get the description of the result 
        $urllink     = $item['guid'];        // Get the link eg amazon.com
        $color       = ($colorCount % 2) ? ROW2_COLOR : ROW1_COLOR; 
        include "resulttemplate.php";
        echo "\n";
        $colorCount++; 
    }
}
else {
    echo "Could not find any results for your search '$search'";
}

Other changes:

  • $start was not declared before your $rss->get("...") call
  • compounded the $color if/else clause into a ternary operation with fewer comparisons
  • I wasn't sure what the purpose of the if ($rs = $rss->get("...")) { } was, so I removed it.

I would also recommend using require instead of include as it will cause a fatal error if resulttemplate.php doesn't exist, which in my opinion is a better way to detect bugs than PHP Warnings which will continue execution. However I don't know you whole situation so it might not be of great use.

Hope that helps!

Cheers

teastburn
  • 2,938
  • 1
  • 20
  • 12