Find out if a site is airborne or not

3

Is there a function in PHP to find out if an IP address (ie website) is in the air or not?

I found this function in WEB but it did not satisfy me .... it takes a lot to load.

function curl_info($url){
    $ch = curl_init();
    curl_setopt( $ch, CURLOPT_URL, $url );
    curl_setopt( $ch, CURLOPT_HEADER, 1);
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
    curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, $timeout );
    curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1 );

    $content = curl_exec( $ch );
    $info = curl_getinfo( $ch );

    return $info;
}

$site = 'http://www.locaweb.com.br';
$info = curl_info( $site );
if( $info['http_code']==200 ) {
    echo '<u>'.$site . '</u> - <strong>está no ar!!</strong><br />';
} else {
    echo '<u>'.$site . '</u> - está fora do ar<br />';
}
    
asked by anonymous 01.09.2014 / 15:10

3 answers

6

1) Using fsockopen , look at the URL format

if( fsockopen( 'www.locaweb.com.br' , 80 , $errno , $errstr , 30 ) ){
    echo 'site online!';
} else {
    echo 'site offline.';
}

2) Using checkdnsrr : Checks the DNS records that match a given host name or Internet IP address .

checkdnsrr( 'google.com' ) // TRUE
checkdnsrr( 'yaruuu.com' ) // FALSE

3) Using curl

$ch = curl_init('http://www.locaweb.com.br');  
curl_setopt($ch, CURLOPT_TIMEOUT, 5);  
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  
$data = curl_exec($ch);  
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);  
curl_close($ch);  

if( $httpcode >= 200 && $httpcode < 300 ){  
    echo 'site online!';
} else {
    echo 'site offline.';
}

In the case of CURL , the URL link returns a status 301 redirect to the link URL, we can accept the site as ONLINE. In the above example it does not consider redirection status as ONLINE , but you can change and accept these cases:

status redirect:

300 => 'Multiple Choices',
301 => 'Moved Permanently',
302 => 'Found', // 1.1
303 => 'See Other',
304 => 'Not Modified',
305 => 'Use Proxy',
// 306 is deprecated but reserved
307 => 'Temporary Redirect',

It is up to you to define the status that will be valid as ONLINE .

    
01.09.2014 / 22:55
1

I had the same problem and changed the script. The "responsibility" of getting page data moved to another function.

Function to check only if the given link returns any response:

//verifica se o link informado retorna alguma resposta
function isUrl($_sUrl){
    $bRet = false;

    $cl = curl_init($_sUrl);
    curl_setopt($cl,CURLOPT_VERBOSE, true);
    curl_setopt($cl,CURLOPT_CONNECTTIMEOUT,1);
    curl_setopt($cl,CURLOPT_HEADER,true);
    curl_setopt($cl,CURLOPT_NOBODY,true);
    curl_setopt($cl,CURLOPT_RETURNTRANSFER,true);
    curl_setopt($cl,CURLOPT_SSL_VERIFYPEER, false);
    $response = curl_exec($cl);
    curl_close($cl);
    if($response) $bRet = true;

    return $bRet;
}

In my case, I check if there is any video on the page:

function getVideo($_sUrl){
    $sVideo = '';
    //necessário a extensão php_openssl activa para ulr's com https
    $html = new DOMDocument();
    @$html->loadHTML(file_get_contents($_sUrl));
    $aMeta = $html->getElementsByTagName('meta');
    foreach($html->getElementsByTagName('meta') as $meta) {
        if($meta->getAttribute('property')=='og:video'){
            $sVideo = $meta->getAttribute('content');
            break;
        }
    }

    return $sVideo;
 }

To use the code above:

$sUrl = 'https://www.youtube.com/watch?v=OZ5gVkSjC8k';
if(isUrl($sUrl))
    $sVideo = getVideo($sUrl);
    
01.09.2014 / 15:51
1

I'm not an expert on the Web part, but I believe that if you run a Get on the address and return successfully, the address is "online"

var urlValida = '/echo/html/';
var urlInvalida =  '/NaoExiste/html/';

$.ajax({    
    type: 'GET',
    url: urlValida,
    success: function (data) {
        alert(urlValida + ' OnLine');
    }
}).error( function () {
        alert(urlValida + 'OffLine');
});

$.ajax({
    type: 'GET',
    url: urlInvalida,
    success: function (data) {
        alert(urlInvalida + ' OnLine');
    }
}).error( function () {
        alert(urlInvalida + 'OffLine');
});

The code can be tested in this example in jsFiddle

    
01.09.2014 / 17:08