雅虎搜索 API 问题

发布于 2024-11-02 23:34:35 字数 1651 浏览 2 评论 0原文

我在使用 yahoo 搜索 API 时遇到问题,有时有效,有时无效,为什么我

在使用此 URL

时遇到问题

http://api. search.yahoo.com/WebSearchService/rss/webSearch.xml?appid=yahoosearchwebrss&query=originurlextension%3Apdf+$search&adult_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/pdfsearchmachine/classes/rss.php 中为 foreach() 提供的参数无效

任何人都可以帮忙..

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜唯美灬不弃 2024-11-09 23:34:35

错误警告:第 14 行 /home4/thesisth/public_html/pdfsearchmachine/classes/rss.php 中为 foreach() 提供的参数无效 意味着 foreach 构造没有收到可迭代对象(通常是数组) 。在您的情况下,这意味着 $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("...")
  • 调用将 $color if/else 子句复合到三元运算中且比较次数较少
  • 之前,未声明 $start我不确定 if 的目的是什么($rs = $rss->get("...")) { } 是,所以我删除了它。

我还建议使用 require 而不是 include 因为如果 resulttemplate.php 不存在,它将导致致命错误,在我看来,这是检测错误的更好方法比 PHP 警告将继续执行。不过我不了解你的全部情况,所以它可能没有多大用处。

希望有帮助!

干杯

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文