10

This is part of a PHP script I am putting together. Basically a domain ($domain1) is defined in a form and a different message is displayed, based on the response code from the server. However, I am having issues getting it to work. The 3 digit response code is all I am interested in.

Here is what I have so far:

function get_http_response_code($domain1) {
    $headers = get_headers($domain1);
    return substr($headers[0], 9, 3);
    foreach ($get_http_response_code as $gethead) { 
        if ($gethead == 200) {
            echo "OKAY!";
        } else {
            echo "Nokay!";
        }
    }
}
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Batfan
  • 7,966
  • 6
  • 33
  • 53
  • Looks like you've got some funny indentation/braces there. – Zaz Aug 04 '10 at 17:29
  • What issues are you having? Please paste any relevant errors that you are getting. Also, assuming the code you pasted accurately reflects your script, none of the code below your `return` statement will execute. – Brian Driscoll Aug 04 '10 at 17:30

4 Answers4

28
$domain1 = 'http://google.com';

function get_http_response_code($domain1) {
  $headers = get_headers($domain1);
  return substr($headers[0], 9, 3);
}

$get_http_response_code = get_http_response_code($domain1);

if ( $get_http_response_code == 200 ) {
  echo "OKAY!";
} else {
  echo "Nokay!";
}
Mchl
  • 61,444
  • 9
  • 118
  • 120
  • 1
    Working for most things, however if the site sends a redirect, then that redirect code is first, i.e `$headers[0] = 301;$headers[5] = 404` which could mean its working or not working. – mt025 Dec 01 '17 at 01:27
4

If you have PHP 5.4.0+ you can use the http_response_code() function. Example:

var_dump(http_response_code()); // int(200)
Andres SK
  • 10,779
  • 25
  • 90
  • 152
  • Not sure why you're answering a 2 year old question (that was answered 2 years ago) but, thanks for the info :) – Batfan Jun 03 '13 at 16:44
  • 4
    oh, i was searching for a http response code related solution and opened this thread. i posted this in case someone arrives here somehow. best regards! – Andres SK Jun 03 '13 at 17:22
  • 5
    `http_response_code()` is for setting the outgoing code (e.g. to the browser), not checking the incoming code (from a server), which is what the OP wanted to do. – CJ Dennis Oct 25 '14 at 00:43
  • 1
    @CJDennis yep, @xmedeko is right, as per the documentation `int http_response_code ([ int $response_code ] )` the `$response_code` parameter is optional. If set, then you change the outgoing code, if blank, you retrieve the current code received by the browser. – Andres SK Feb 05 '15 at 15:38
  • 10
    @andufo This doesn't work for what the OP asked for. This will only affect the response code of the code that is going to return to the browser. The OP wanted to get the headers from another website entirely thus your answer is wrong entirely. – Jonathan Mar 20 '17 at 15:36
0

Here is my solution for people who need send email when server down:

$url = 'http://www.example.com';

while(true) {

    $strHeader = get_headers($url)[0];

    $statusCode = substr($strHeader, 9, 3 );

    if($statusCode != 200 ) {
        echo 'Server down.';
        // Send email 
    }
    else {
        echo 'oK';
    }

    sleep(30);
}
christian Nguyen
  • 1,530
  • 13
  • 11
0

You directly returned so function wont execute further foreach condition which you written. Its always better to maintain two functions.

function get_http_response_code($domain1) {
    $headers = get_headers($domain1);
    return substr($headers[0], 9, 3); //**Here you should not return**
    foreach ($get_http_response_code as $gethead) { 
        if ($gethead == 200) {
            echo "OKAY!";
        } else {
            echo "Nokay!";
        }
    }
}
RamaKrishna
  • 219
  • 1
  • 8