note 65631 added to function.fsockopen
| From: | yourpicture at hotpop dot com | Date: | Thu, 04 May 2006 18:30:32 +0000 |
| Subject: | note 65631 added to function.fsockopen | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-110564@lists.php.net to get a copy of this message | ||
When your connection times out the server may issue Fatal error: Maximum execution time of 30
seconds exceeded in ...
To get around this try this method used with error handling and copied method for ping operation.
<?php
function Ping(){
// false proxy used to generate connection error
$ProxyServer = "116.155.95.163";
$ProxyPort = 8080;
$timeout=10;
echo "Opening ProxyServer $ProxyServer<br>";
// must use next two statements
Set_Time_Limit(0); //Time for script to run .. not sure how it works with 0 but you need it
Ignore_User_Abort(True); //this will force the script running at the end
$handle = fsockopen($ProxyServer, $ProxyPort,$errno,$errstr,$timeout);
if (!$handle){
echo "Failed to open ProxyServer $ProxyServer errno=$errno,errstr=$errstr<br>";
return 0;
}
else {
// copied method for PING like time operation
$status = socket_get_status($handle);
echo "Opened ProxyServer $ProxyServer<br>";
//Time the responce
list($usec, $sec) = explode(" ", microtime(true));
$start=(float)$usec + (float)$sec;
$timeout=120;
stream_set_timeout($handle,$timeout);
//send somthing
ini_set('display_errors','0');
$write=fwrite($handle,"echo this\n");
if(!$write){
return 0;
}
echo "Try To Read<br>";
stream_set_blocking($handle,0);
//Try to read. the server will most likely respond with a "ICMP Destination
Unreachable" and end the read. But that is a responce!
fread($handle,1024);
fclose($handle);
echo "Read<br>";
ini_set('display_errors','1');
//Work out if we got a responce and time it
list($usec, $sec) = explode(" ", microtime(true));
$laptime=((float)$usec + (float)$sec)-$start;
if($laptime>$timeout)
return 0;
//else
// $laptime = round($laptime,3);
return $laptime;
}
}
// must use ErrorHandler to avoid php error being printed to screen
function userErrorHandler($errno, $errmsg, $filename, $linenum, $vars)
{
// you can set this to what ever you like.
echo "In Error Handler<br>";
return 0;
}
$old_error_handler = set_error_handler("userErrorHandler");
$time = Ping();
echo "Time=$time<br>";
echo "Done Checking<br>";
?>
Response Will be:
Opening ProxyServer 116.155.95.163
In Error Handler
Failed to open ProxyServer 116.155.95.163 errno=10060,errstr=A connection attempt failed
because the connected party did not properly respond after a period of time, or
established connection failed because connected host has failed to respond.
Time=0
Done Checking
Thanks to everyone for your source code examples.
AndE
----
Server IP: 216.235.15.211
Probable Submitter: 209.135.109.141
----
X-Spam-Status: No, hits=3.3 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER,
HTML_MESSAGE autolearn=no version=2.64
----
Manual Page -- http://www.php.net/manual/en/function.fsockopen.php
Edit -- http://master.php.net/note/edit/65631
Del: integrated -- http://master.php.net/note/delete/65631/integrated
Del: useless -- http://master.php.net/note/delete/65631/useless
Del: bad code -- http://master.php.net/note/delete/65631/bad+code
Del: spam -- http://master.php.net/note/delete/65631/spam
Del: non-english -- http://master.php.net/note/delete/65631/non-english
Del: in docs -- http://master.php.net/note/delete/65631/in+docs
Del: other reasons-- http://master.php.net/note/delete/65631
Reject -- http://master.php.net/note/reject/65631
Search -- http://master.php.net/manage/user-notes.php