I am getting this error ,can anyone help me :

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

Here is the rss.php

code

<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("")) 
{

}

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

} 

	



?>

Dani AI

Generated

Quick diagnosis and what the error means — the PHP warning "Invalid argument supplied for foreach()" simply means the value you put inside foreach() is not an array or Traversable object. In this thread the loop over $rs['items'] is failing because $rs (or $rs['items']) is coming back as false, null, a string, or a SimpleXML object instead of an array. Confirm the actual type before iterating. (php.net)

Practical, low-risk checks to add now: log the raw fetch result, check HTTP response code, then parse only when you have valid XML/array. The sketch below shows the flow (use your $rss->get() or a cURL wrapper — check HTTP code with curl_getinfo() and parse with simplexml_load_string() when appropriate):

$raw = $rss->get($url); // or fetch via cURL and return body
if ($raw === false) {
    error_log("fetch failed");
} elseif ($xml = @simplexml_load_string($raw)) {
    foreach ($xml->channel->item as $it) {
        // process SimpleXMLElement item
    }
} elseif (is_array($raw) && !empty($raw['items'])) {
    foreach ($raw['items'] as $item) {
        // process associative-array item
    }
} else {
    // unexpected format: log and fail gracefully
}

Always build the query string with proper URL-encoding instead of manual str_replace of spaces. Use urlencode() (or rawurlencode() when RFC 3986 encoding is required) and intval() for numeric params like start. Test the final URL from the server (curl or browser) to see raw XML and status codes. (php.net)

Why it is intermittent: is right that the endpoint can return errors; is also right about intermittent outages during load or API changes. Historically Yahoo’s public search services had free limits (commonly cited around 5,000/day) and later moved toward paid/changed services (BOSS/Yahoo developer changes), so intermittent failures, different quotas, or deprecation are possible — consider caching results, exponential backoff on 5xx responses, and moving to a supported provider if reliability is required. For the RSS/discontinued reports and the historical limit info see these references. (stackoverflow.com)

Summary action list: (1) add type checks and logging before foreach, (2) curl the URL and inspect HTTP code + body, (3) use urlencode() when building queries, (4) add caching + retry/backoff, and (5) consider migrating to a supported search API if outages persist.

Recommended Answers

All 7 Replies

your above is not returning proper output, there is error on that server

your above is not returning proper output, there is error on that server

I am using the same URL with another website and it is working..how i can correct that..

For php error,
Always put foreach in if condition.

if(count($rs['items'])>0)
{
	foreach($rs['items'] as $item) 
	{ 
		//--- code---
	}
}
else
{
	echo 'There is no items...';
}

And for URL there are some php variables in url like $search.
Check the string generated (output of php code) in the one you have used in another site.

For php error,
Always put foreach in if condition.

if(count($rs['items'])>0)
{
	foreach($rs['items'] as $item) 
	{ 
		//--- code---
	}
}
else
{
	echo 'There is no items...';
}

And for URL there are some php variables in url like $search.
Check the string generated (output of php code) in the one you have used in another site.

Someone told me that maximum number of searches per day by yahoo is only 5000 ,if you want to advance that ,you need to pay..is that true?

For php error,
Always put foreach in if condition.

if(count($rs['items'])>0)
{
	foreach($rs['items'] as $item) 
	{ 
		//--- code---
	}
}
else
{
	echo 'There is no items...';
}

And for URL there are some php variables in url like $search.
Check the string generated (output of php code) in the one you have used in another site.

It is working with that URL ,previuos one ,I did not do any changes,Sometimes it works and sometimes don't ,I don't know why..

It might be the api is not responding correctly...this problem exist sometimes due to heavy load or change(updation or modification) in api.....

So ,What you think the

this API is temporary blockage or permanent as Yahoo said it is going to stop some of it's services..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.