dthh5038 2011-04-22 05:43
浏览 66

Yahoo Search API问题

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 "
";  
    }  
 ?>

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..

  • 写回答

1条回答 默认 最新

  • doubinduo3364 2011-04-22 21:24
    关注

    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 "
    ";
            $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

    评论

报告相同问题?

悬赏问题

  • ¥15 r语言神经网络自变量重要性分析
  • ¥15 基于双目测规则物体尺寸
  • ¥15 wegame打不开英雄联盟
  • ¥15 公司的电脑,win10系统自带远程协助,访问家里个人电脑,提示出现内部错误,各种常规的设置都已经尝试,感觉公司对此功能进行了限制(我们是集团公司)
  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢