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!";
        }
    }
}
2
  • Looks like you've got some funny indentation/braces there. Commented Aug 4, 2010 at 17:29
  • 1
    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. Commented Aug 4, 2010 at 17:30

4 Answers 4

30
$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!";
}
Sign up to request clarification or add additional context in comments.

1 Comment

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.
0

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

var_dump(http_response_code()); // int(200)

4 Comments

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.
@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.
@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 I have reviewed this question. and it looks like you might be the one who does not understand. All indications are that there is exactly one URL/site in question; there was nothing stated about "headers from another website" nor anything like that. that I can see.
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);
}

Comments

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!";
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.