imperium2335 Posted July 10, 2009 Share Posted July 10, 2009 Is there are way to make the get_file_contents function time out if the target site is not responding, and then move onto the next? At the moment my script just times out because of the typical php timout. Thanks in advance. Link to comment https://forums.phpfreaks.com/topic/165437-get-file-contents-time-out/ Share on other sites More sharing options...
Daniel0 Posted July 10, 2009 Share Posted July 10, 2009 Yeah, just pass it a context resource specifying the timeout value (it's in the manual). Link to comment https://forums.phpfreaks.com/topic/165437-get-file-contents-time-out/#findComment-872604 Share on other sites More sharing options...
imperium2335 Posted July 10, 2009 Author Share Posted July 10, 2009 ive tried that but its not working, just behaving as normal $ctx = stream_context_create(array('http' => array('timeout' <= 1))); $var= @file_get_contents($thisone, 0, $ctx) ; Link to comment https://forums.phpfreaks.com/topic/165437-get-file-contents-time-out/#findComment-872633 Share on other sites More sharing options...
Daniel0 Posted July 10, 2009 Share Posted July 10, 2009 That's because you defined your array incorrectly. <= is a comparison operator, so what you will have is a in the 'http' index is an array containing the boolean value of 'timeout' <lower than or equal to> 1. What you're looking for is: $ctx = stream_context_create(array('http' => array('timeout' => 1))); Link to comment https://forums.phpfreaks.com/topic/165437-get-file-contents-time-out/#findComment-872634 Share on other sites More sharing options...
imperium2335 Posted July 10, 2009 Author Share Posted July 10, 2009 that does not work either Link to comment https://forums.phpfreaks.com/topic/165437-get-file-contents-time-out/#findComment-872637 Share on other sites More sharing options...
Daniel0 Posted July 10, 2009 Share Posted July 10, 2009 "Doesn't work" doesn't help me help you. It "works" for me. I have test2.php looking like this: <?php sleep(10); echo 'hi'; And another file looking like this: <?php $ctx = stream_context_create(array('http' => array('timeout' => 1))); $var= file_get_contents('http://localhost/test2.php', 0, $ctx); echo $var; Which results in Warning: file_get_contents(http://localhost/test2.php) [function.file-get-contents]: failed to open stream: HTTP request failed! in C:\xampp\htdocs\test.php on line 4 As you would expect. If I set timeout to a value higher than 10 I'll get the 'hi' output. Link to comment https://forums.phpfreaks.com/topic/165437-get-file-contents-time-out/#findComment-872640 Share on other sites More sharing options...
imperium2335 Posted July 10, 2009 Author Share Posted July 10, 2009 so timeout is just a variable i need to declare? at the moment i dont have that. Link to comment https://forums.phpfreaks.com/topic/165437-get-file-contents-time-out/#findComment-872642 Share on other sites More sharing options...
imperium2335 Posted July 10, 2009 Author Share Posted July 10, 2009 what i want is the function to time out if the url is taking to long to respond, so the loop can continue and move onto the next url (it is looping through a list of urls and retrieving the content). Link to comment https://forums.phpfreaks.com/topic/165437-get-file-contents-time-out/#findComment-872644 Share on other sites More sharing options...
Daniel0 Posted July 10, 2009 Share Posted July 10, 2009 Right, and that is possible. The timeout is a float. If the time spent on retrieving the resource is larger than that float value it will fail with a Warning as demonstrated in the example. Just set the timeout to the maximum acceptable time for you. Link to comment https://forums.phpfreaks.com/topic/165437-get-file-contents-time-out/#findComment-872648 Share on other sites More sharing options...
imperium2335 Posted July 10, 2009 Author Share Posted July 10, 2009 but wont the sleep() function cause the whole script to pause? Link to comment https://forums.phpfreaks.com/topic/165437-get-file-contents-time-out/#findComment-872652 Share on other sites More sharing options...
Daniel0 Posted July 10, 2009 Share Posted July 10, 2009 The sleep() I added in another file was just to make sure it took long enough for the file retrieval to timeout. It was to emulate high network latency. Link to comment https://forums.phpfreaks.com/topic/165437-get-file-contents-time-out/#findComment-872655 Share on other sites More sharing options...
imperium2335 Posted July 10, 2009 Author Share Posted July 10, 2009 sorry, I still dont understand Where is the length of timeout coming from? $ctx = stream_context_create(array('http' => array('timeout' => 1))); $currentblog = @file_get_contents($thisone, 0, $ctx) ; or sleep(10)? Have tried everything you have said so far but it doesn't work Link to comment https://forums.phpfreaks.com/topic/165437-get-file-contents-time-out/#findComment-872661 Share on other sites More sharing options...
Daniel0 Posted July 10, 2009 Share Posted July 10, 2009 $ctx = stream_context_create(array('http' => array('timeout' => 1))); You set it there, to whatever value you want. Ignore sleep(), that was for my testing purposes in a different script. Link to comment https://forums.phpfreaks.com/topic/165437-get-file-contents-time-out/#findComment-872663 Share on other sites More sharing options...
imperium2335 Posted July 10, 2009 Author Share Posted July 10, 2009 i thought u said that => 1 was boolean for true? as in 0 is false? it is still not working though thanks for trying so far. Link to comment https://forums.phpfreaks.com/topic/165437-get-file-contents-time-out/#findComment-872667 Share on other sites More sharing options...
Daniel0 Posted July 10, 2009 Share Posted July 10, 2009 No, you did <=, which is a binary operator that returns a boolean value. I did =>, which is quite different. You might want to read the Language Reference chapter in the manual. Again, saying "it doesn't work" really isn't of much help to me. Doesn't work can mean hundreds of different things. You need to tell me how it doesn't work, i.e., what did you expect and what happened instead? Link to comment https://forums.phpfreaks.com/topic/165437-get-file-contents-time-out/#findComment-872671 Share on other sites More sharing options...
MadTechie Posted July 10, 2009 Share Posted July 10, 2009 Yes it is.. works for me Here's an idea maybe, if you give a little bit of useful information or here's an idea maybe post what you have and a description of what you want it to do and what it is or is not doing, you know considing we are spending time to help maybe a little bit of your time would be a valid investment Link to comment https://forums.phpfreaks.com/topic/165437-get-file-contents-time-out/#findComment-872672 Share on other sites More sharing options...
imperium2335 Posted July 10, 2009 Author Share Posted July 10, 2009 it is going through a list of addresses and assigning the output to a variable which is then analysed. It then repeats for the next address in the list. But if an address is taking forever to respond the script will time out (as expected) so it doesn't scan the rest of the addresses in the list after the one that takes forever and never loads. Link to comment https://forums.phpfreaks.com/topic/165437-get-file-contents-time-out/#findComment-872676 Share on other sites More sharing options...
Daniel0 Posted July 10, 2009 Share Posted July 10, 2009 Right, something like <?php $addresses = array( 'http://foo.com/something.php', 'http://example.com/helloWorld.py', ); $context = stream_context_create(array('http' => array('timeout' => 1))); $values = array(); $oldErrorReporting = error_reporting(0); // faster than using @ on each iteration foreach ($addresses as $address) { $var = file_get_contents($address, $context); } error_reporting($oldErrorReporting); If something takes over 1 second it will be skipped as per the timeout value. Link to comment https://forums.phpfreaks.com/topic/165437-get-file-contents-time-out/#findComment-872677 Share on other sites More sharing options...
MadTechie Posted July 10, 2009 Share Posted July 10, 2009 Maybe worth noting Whats the timeout for the script.. if each site took 1 second and you had 60 sites, but your timeout was 30 then the script will timeout, with that inmind it may be worth adding set_time_limit(0); Also $var = file_get_contents($address, $context); should probably be something like $var = file_get_contents($address, $context); $values[] = ($var===false)?false:true; Link to comment https://forums.phpfreaks.com/topic/165437-get-file-contents-time-out/#findComment-872688 Share on other sites More sharing options...
imperium2335 Posted July 10, 2009 Author Share Posted July 10, 2009 Hi Mad, once a sites contents are retrieved successfully, 30 seconds is added onto the time out. Link to comment https://forums.phpfreaks.com/topic/165437-get-file-contents-time-out/#findComment-872837 Share on other sites More sharing options...
MadTechie Posted July 10, 2009 Share Posted July 10, 2009 If you say so.. theirs no way i could know thast with seeing the code! Link to comment https://forums.phpfreaks.com/topic/165437-get-file-contents-time-out/#findComment-872840 Share on other sites More sharing options...
imperium2335 Posted July 10, 2009 Author Share Posted July 10, 2009 $Target_Fragment = $_POST['frag'] ; if($Target_Fragment == NULL) header("Location: default.html") ; $Scan_Time_Limit = $_POST['tout'] ; // The maximum time allowed to scan one site. echo "<head><style type=\"text/css\"> #probar {width:136px;height:16px;background:url('probar.gif') bottom no-repeat ;*background:url('probar.gif') top no-repeat ;} </style></head>" ; $ext = explode(".", ($_FILES['upload']['name'])) ; $ext = $ext[1] ; if($ext != "txt") { echo "File must be a TXT file, <a href=\"default.html\">go back</a> and try again." ; exit() ; } else $listfile = ($_FILES['upload']['tmp_name']); $ctx = stream_context_create(array('http' => array('timeout' => 3))); $pb = 0 ; $stuff = file_get_contents($listfile) ; $urls = explode("http://", $stuff) ; $stuffcount = count($urls)-1 ; $found = 0 ; $notfound = 0 ; $scanned = 0 ; $successes = array() ; $fails = array() ; array_shift($urls) ; $a=0;$b=0;$c=0;$d=0;$e=0;$f=0;$g=0;$h=0;$ip=0;$j=0;$int=0; //Progress bar stops. echo "Scanning $stuffcount from the uploaded text file " . ($_FILES['upload']['name']) . ".<br/>" ; echo "Standby" ; for($i = 0; $i < $stuffcount; $i++) { $thisone = "http://" . $urls[$i] ; set_time_limit($Scan_Time_Limit) ; $currentitem = @file_get_contents($thisone, $ctx) ; $bool = stristr($currentitem, $Target_Fragment) ; if($bool == TRUE) { $found++ ; array_push($successes, $urls[$i]) ; } else { $notfound++ ; array_push($fails, $urls[$i]) ; } $scanned++ ; $pcomplete = ceil((($scanned / $stuffcount) * 100)) ; if($int == 0) echo "<div id=\"probar\">" ; $int = 1 ; if($pcomplete > 0 & $a == 0){echo "<img src=\"pip.jpg\"> " ; $a = 1;} if($pcomplete > 20 & $b == 0){echo "<img src=\"pip.jpg\"> " ; $b = 1;} if($pcomplete > 30 & $c == 0){echo "<img src=\"pip.jpg\"> " ; $c = 1;} if($pcomplete > 40 & $d == 0){echo "<img src=\"pip.jpg\"> " ; $d = 1;} if($pcomplete > 50 & $e == 0){echo "<img src=\"pip.jpg\"> " ; $e = 1;} if($pcomplete > 60 & $f == 0){echo "<img src=\"pip.jpg\"> " ; $f = 1;} if($pcomplete > 70 & $g == 0){echo "<img src=\"pip.jpg\"> " ; $g = 1;} if($pcomplete > 80 & $h == 0){echo "<img src=\"pip.jpg\"> " ; $h = 1;} if($pcomplete > 90 & $ip == 0){echo "<img src=\"pip.jpg\"> " ; $ip = 1;} if($pcomplete > 95 & $j == 0){echo "<img src=\"pip.jpg\"></div>" ; $j = 1;} } echo "<p>Scan complete</p>" . "<p>$scanned were checked for the code fragment: $Target_Fragment </p>" . "<p>$found contain the fragment.</p><p>$notfound do not contain the fragment.</p>" ; echo " <table width=\"1000\" border=\"1\"> <tr><td><strong>URL</strong></td><td><strong>Found?</strong></td> " ; $i = 0 ; $x = 0 ; for($z = 0; $z < $scanned; $z++) { echo "<tr><td width=\"500\"><a href=\"http://$urls[$z]\" target=\"_blank\">$urls[$z]</a>" ; if(in_array($urls[$z], $successes)) echo "<td><strong>Found!</strong></td></tr>" ; else echo "<td width=\"500\"></td></tr>" ; } echo "</table>" ; Link to comment https://forums.phpfreaks.com/topic/165437-get-file-contents-time-out/#findComment-872889 Share on other sites More sharing options...
imperium2335 Posted July 11, 2009 Author Share Posted July 11, 2009 Any idea why the timeout still wont work? Link to comment https://forums.phpfreaks.com/topic/165437-get-file-contents-time-out/#findComment-873361 Share on other sites More sharing options...
Daniel0 Posted July 11, 2009 Share Posted July 11, 2009 Seriously, what do you mean with "it doesn't work"? It can mean hundreds of things. Link to comment https://forums.phpfreaks.com/topic/165437-get-file-contents-time-out/#findComment-873377 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.