andybee Posted March 13, 2007 Share Posted March 13, 2007 Hi, I am trying to get info from a several websites (10 web sites), by loading the websites into a variable by using the method file(). It works fine to load one site into the variable, but when I am trying to load the web sites by using a while loop, I get a warning which says: "function.file]: failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found in" My php code with while loop look like this: <?php $siteCode=1; while($siteCode<10){ $lines = file("http://www.page.com/site=".$siteCode); $line1=$lines[1]; $line2=$lines[2]; echo $line1; echo $line2; $siteCount++; } ?> If I am not using the while loop it works fine, but I can not get info from all the websites. Without while loop the php code look like this: <?php $siteCode=1; $lines = file("http://www.page.com/site=".$siteCode); $line1=$lines[1]; $line2=$lines[2]; echo $line1; echo $line2; Do any of you know how I can solve this problem! Thank you all! Andybee. Link to comment https://forums.phpfreaks.com/topic/42482-while-loop-and-file/ Share on other sites More sharing options...
dymon Posted March 13, 2007 Share Posted March 13, 2007 Hi, I think one of your sites is not working or has some other problems. You can try the sites one by one using: $siteCode=1;//2,3,4,... $lines = file("http://www.page.com/site=".$siteCode); $line1=$lines[1]; $line2=$lines[2]; echo $line1; echo $line2; And to see what site gives the error and than try to repair it. Dymon Link to comment https://forums.phpfreaks.com/topic/42482-while-loop-and-file/#findComment-206133 Share on other sites More sharing options...
mattd8752 Posted March 13, 2007 Share Posted March 13, 2007 function sitetofile($site){ $data = file($site); $return = "Now showing: ".$site."<br>\n"; $x = 0; while($x < sizeof($data)){ $return .= $data[$x]; } } $google = sitetofile("www.google.com"); echo $google; Is that not what you need in a much simpler way? Link to comment https://forums.phpfreaks.com/topic/42482-while-loop-and-file/#findComment-206159 Share on other sites More sharing options...
andybee Posted March 13, 2007 Author Share Posted March 13, 2007 Hi, dymon: www.page.com/site=1 www.page.com/site=2 .... www.page.com/site=10 do not exist. I used them only to illustrate that I want the counter in the loop to select the websites. Matt: When I tryed your code, I got following message: Warning: file(www.google.com) [function.file]: failed to open stream: No such file or directory in file.php Can someone please help me with this problem! AndyBee. Link to comment https://forums.phpfreaks.com/topic/42482-while-loop-and-file/#findComment-206223 Share on other sites More sharing options...
boo_lolly Posted March 13, 2007 Share Posted March 13, 2007 why don't you store the urls in an array, and use a foreach loop? Link to comment https://forums.phpfreaks.com/topic/42482-while-loop-and-file/#findComment-206231 Share on other sites More sharing options...
andybee Posted March 13, 2007 Author Share Posted March 13, 2007 boo_lolly: because I dont know how many websites I am going to look in. There will be about 100.000 websites, so if i store all this numbers I will use a lot of space in my sql db.... Andybee Link to comment https://forums.phpfreaks.com/topic/42482-while-loop-and-file/#findComment-206247 Share on other sites More sharing options...
boo_lolly Posted March 13, 2007 Share Posted March 13, 2007 this is your while loop: <?php $siteCode=1; while($siteCode<10){ $lines = file("http://www.page.com/site=".$siteCode); $line1=$lines[1]; $line2=$lines[2]; echo $line1; echo $line2; $siteCount++; } ?> this is what it should look like: <?php $siteCode=1; while($siteCode < 10){ $lines = file("http://www.page.com/site=".$siteCode); $line1=$lines[1]; $line2=$lines[2]; echo $line1; echo $line2; $siteCode++; } ?> or you could do it the way i'd do it: <?php for($i = 1; $i < 10; $i++){ $lines = file("http://www.page.com/site=".$siteCode); echo $lines[1] ."<br />\n"; echo $lines[2] ."<br />\n"; } ?> Link to comment https://forums.phpfreaks.com/topic/42482-while-loop-and-file/#findComment-206286 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.