HTTP verification (200) in PHP

0

I did my verification like this, except that in mine it gave an error and in other systems of verification did not give. I wonder where did I go wrong?

 $host = 'IP do HOST';
 $conectado = @fsockopen($host, 80);

 if (is_resource($conectado)) {
    print 'online';
 }else{
    print 'offline';
 }

Below my system and another verified system.

    
asked by anonymous 26.02.2018 / 19:54

1 answer

0

To use fsockopen , you need to remove schema ( http:// ) and the slash at the end.

Example:

$host = 'pt.stackoverflow.com';
$conectado = fsockopen($host, 80);

if (is_resource($conectado)) {
    print 'online';
}else{
    print 'offline';
}

Alternatives

In addition to fsockpopen , you can also use fopen and curl .

Example with fopen :

$host = 'https://pt.stackoverflow.com/';
$conectado = fopen($host, 'r');

@$result = @stream_get_meta_data($conectado);

if (is_resource($conectado) && strpos($result['wrapper_data'][0], "200") !== false) {
    print 'online';
}else{
    print 'offline';
}

Example with curl :

$host = 'http://pt.stackoverflow.com:80';

ob_start();
$ch = curl_init($host);
curl_exec($ch);
$info = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
ob_end_clean();

if( $info === 200 ) {
    echo "Offline";
} else {
    echo "Offline";
}
    
26.02.2018 / 21:23