Сокеты на php (help)
Все что нашел, это код вот такой функции, пингования прокси на PHP,
но чет не работает, юзал на беспл хосте... хост в порядке если вы об этом хотели узнать.
Код:
<? 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>";
?>
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>";
?>
Все зависит от того, какой тип прокси будешь юзать: HTTP,HTTPS,SOCKS4(5). В первом случае конектишься fsocket по IP и порту и отправляешь HTTP запрос, с HTTPS посложнее, нужно конектится по IP и порту и отправлять CONNECT IP:PORT и смотреть на ответ. C соксами еще сложнее смотри RFC.