Php 雅虎搜索API问题

Php 雅虎搜索API问题,php,yahoo-search,Php,Yahoo Search,我对yahoo搜索API有问题,有时有效,有时无效,为什么我对它有问题 我正在使用这个URL $search&成人_ok=1&start=$start 代码如下: <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <? $search = $_GET["search"]; $replace = " "; $with = "+"; $search = str_replace($repl

我对yahoo搜索API有问题,有时有效,有时无效,为什么我对它有问题

我正在使用这个URL

$search&成人_ok=1&start=$start

代码如下:

<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";  
    }  
 ?>
有时它会给出结果,有时则不会。我通常会犯这个错误

警告:第14行的/home4/thesisth/public_html/pdfsearchcmachine/classes/rss.php中为foreach提供的参数无效

任何人都可以帮助..

错误警告:在第14行的/home4/thesisth/public\u html/pdfsearchmachine/classes/rss.php中为foreach提供的参数无效,这意味着foreach构造没有接收到一个iterable数组,通常是数组。在你的例子中,这意味着$rs['items']是空的。。。也许搜索没有结果

我建议在$rss->get的结果中添加一些检查。。。首先,当请求失败或未返回结果时,还有一个操作:

<?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'";
}
其他变化:

在$rss->get之前未声明$start。。。呼叫 将$color if/else子句组合成一个三元操作,比较更少 我不确定if$rs=$rss->get的目的是什么。。。{}是,所以我把它移走了。 我还建议使用require而不是include,因为如果resulttemplate.php不存在,它将导致致命错误,我认为这是比继续执行的php警告更好的检测错误的方法。但是我不知道你的整个情况,所以它可能没有多大用处

希望有帮助


干杯

我希望通过标准化格式来提高代码的可读性@Amit,您可以使用3个以上的标记,如果您包括您在这里使用的编程语言,您可能会得到更多帮助。祝你好运