FOREACH Loop Problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Big Squid
Forum Newbie
Posts: 2
Joined: Tue Mar 29, 2011 10:14 am

FOREACH Loop Problem

Post 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...
mikecampbell
Forum Commoner
Posts: 38
Joined: Tue Oct 12, 2010 7:26 pm

Re: FOREACH Loop Problem

Post by mikecampbell »

What are the errors that you receive?
Big Squid
Forum Newbie
Posts: 2
Joined: Tue Mar 29, 2011 10:14 am

Re: FOREACH Loop Problem

Post 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]
mayder
Forum Newbie
Posts: 1
Joined: Thu Mar 05, 2015 6:21 am

Re: FOREACH Loop Problem

Post by mayder »

How do you solve this problem?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: FOREACH Loop Problem

Post by Christopher »

You are getting a 503 Service Unavailable error for one of the URLs. Perhaps you should check if the URLs exist first?
(#10850)
Post Reply