서버의 상태가 느린지 다운되었는지 등을 체크하기 위해 보통 ping을 이용합니다. 서버 응답이 없으면 -1를 반환하고 응답이 있으면 지연율을 반환합니다.

<?php
 function pingDomain($domain)
 {
    $starttime = microtime(true);
    $file      = fsockopen ($domain, 80, $errno, $errstr, 10);
    $stoptime  = microtime(true);
    $status    = 0;

    if(!$file)
    {
        $status = -1;
    }
    else
    {
        fclose($file);
        $status = ($stoptime - $starttime) * 1000;
        $status = floor($status);
    }
    return $status . ' ms';
 }


 print_r(pingDomain('habonyphp.com'));
 // 결과 : 829 ms
 ?>

출처: http://stackoverflow.com/questions/9841635/how-can-i-ping-a-server-port-with-php

0 댓글