HTTP(200) verification in PHP

Asked

Viewed 93 times

0

I did my checking so, only in mine gave an error and in other verification systems did not give. I wonder where it was that I went wrong?

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

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

Below my system and another system checked.

Meu sistema

Sistema verificado

  • You are using curl_init ? Or you know if your page is returning the status 200?

  • 'Cause it’s I tried to make via CURL plus my trial hosting doesn’t allow I guess.

  • I want to send a command to a page to return the value 200.

  • Utilize curl_getinfo

  • Neither curl_getinfo(); as tbm does not appear phpinfo();

  • error 404 on page.

  • Have you tried to verify the answer with the stream_get_meta_data ?

  • How do I do that?

  • If possible [Edit] your question and add how you are doing. It becomes easier to show a solution.

  • So I changed, I tried to be as clear as I could.

  • In my system only returned at 14:40 on the other verification system nor crashed.

Show 6 more comments

1 answer

0


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

Example:

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

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

Alternatives

Beyond the fsockpopen, you can also use fopen and curl.

Example with fopen:

$host = '/';
$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://answall.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";
}
  • I was wrong, I will change the question. Pq on my system has no "/". And with fopen and Curl does not work on my test system.

  • I got it, I was really missing something in my code yours made me see. Thank you all for your help

Browser other questions tagged

You are not signed in. Login or sign up in order to post.