Page 1 of 1

FOREACH Loop Problem

Posted: Tue Mar 29, 2011 10:30 am
by Big Squid
I'm trying to write a script that will check to see if a specifc url is indexed within Google, Yahoo, & Bing.

I've got it to work using a single url, however when I add another url, the problems begin. It doesn't matter if I'm checking 2 or 200 urls, only the last url on the list will work. The others return errors.

Here's my FOREACH LOOP statement (after my variables are set):

Code: Select all

                                $handle_google	= fopen("$google", "r");
				$contents_google = stream_get_contents($handle_google);
				fclose($handle_google);
				if(strstr($contents_google, $needle) !== false) {
					echo '<a href="'.$google.'" target="_blank">found</a>';
				}
				else {
					echo 'not found';
				}
				echo '</td><td>';
				
				$handle_yahoo = fopen("$yahoo", "r");
				$contents_yahoo = stream_get_contents($handle_yahoo);
				fclose($handle_yahoo);
				if(strstr($contents_yahoo, $needle_yahoo) !== false) {
					echo '<a href="'.$yahoo.'" target="_blank">found</a>';
				}
				else {
					echo 'not found';
				}
				echo '</td><td>';
				
				$handle_bing = fopen("$bing", "r");
				$contents_bing = stream_get_contents($handle_bing);
				fclose($handle_bing);
				if(strstr($contents_bing, $needle_bing) !== false) {
					echo '<a href="'.$bing.'" target="_blank">found</a>';
				}
				else {
					echo 'not found';
				}
I know the variables work (and the code works) for one single url. I just can't seem to get the rest to work.

Any help will be gratefully received...

Re: FOREACH Loop Problem

Posted: Tue Mar 29, 2011 11:41 am
by mikecampbell
What are the errors that you receive?

Re: FOREACH Loop Problem

Posted: Tue Mar 29, 2011 12:29 pm
by Big Squid
Here is an example of the errors. In this case I entered the exact same url, and only the last iteration worked.

[text]Warning: fopen(http://www.google.com/search?q=site:htt ... google.com ) [function.fopen]: failed to open stream: HTTP request failed! HTTP/1.0 503 Service Unavailable in C:\xampp\htdocs\rss\rss.php on line 61

Warning: stream_get_contents() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\rss\rss.php on line 62

Warning: fclose() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\rss\rss.php on line 63
URL Google Yahoo Bing
http://www.google.com not found not found not found
http://www.google.com found found found[/text]

Re: FOREACH Loop Problem

Posted: Thu Mar 05, 2015 6:23 am
by mayder
How do you solve this problem?

Re: FOREACH Loop Problem

Posted: Thu Mar 05, 2015 9:18 am
by Christopher
You are getting a 503 Service Unavailable error for one of the URLs. Perhaps you should check if the URLs exist first?