雅虎搜索 API 问题
我在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
错误警告:第 14 行 /home4/thesisth/public_html/pdfsearchmachine/classes/rss.php 中为 foreach() 提供的参数无效 意味着 foreach 构造没有收到可迭代对象(通常是数组) 。在您的情况下,这意味着
$rs['items']
为空...也许搜索没有返回结果?我建议首先对
$rss->get("...")
的结果添加一些检查,并且在请求失败或不返回结果时执行操作:其他更改:
$rss->get("...")
$color
if/else 子句复合到三元运算中且比较次数较少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:Other changes:
$rss->get("...")
call$color
if/else clause into a ternary operation with fewer comparisonsif ($rs = $rss->get("...")) { }
was, so I removed it.I would also recommend using
require
instead ofinclude
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