How can you measure the response time for each target when using fsockopen in PHP?
When using fsockopen in PHP to connect to multiple targets, you can measure the response time for each target by recording the time before and after sending the request, then calculating the difference. This can help you identify any slow or unresponsive targets in your network.
<?php
$targets = array('example.com', 'google.com', 'facebook.com');
foreach ($targets as $target) {
$start = microtime(true);
$fp = fsockopen($target, 80, $errno, $errstr, 5);
if ($fp) {
fwrite($fp, "GET / HTTP/1.0\r\nHost: $target\r\n\r\n");
while (!feof($fp)) {
fgets($fp, 128);
}
fclose($fp);
}
$end = microtime(true);
$responseTime = $end - $start;
echo "Response time for $target: " . $responseTime . " seconds\n";
}
?>
Related questions
-
try-catch error_reporting debugging tools
-
PHP day of the week date
-
object-oriented programming PHP class properties