How to get the Status Code from HTTP Response Headers in PHP

On 8/18/2014

We'll see how to get the Status Code of any HTTP RESPONSE Headers sent by the server upon a HTTP REQUEST in PHP. For example, opening a webpage in a browser will send a HTTP request to the server and which in turn will be returned by a response. At times, we want to get the status code alone from the headers sent by the server for processing.

status-code-http-request-response-headers-php

Recommended Read: How to Create Login Form in CodeIgniter, MySQL and Twitter Bootstrap

Recommended Read: How to add jQuery UI Slider Input Control to HTML Form

Get the HTTP Response Status Code

First use the PHP get_headers() function to fetch the array of all the headers sent by the server and then strip the status code from it.

<?php
//function to get the http reponse code
function get_response_code($url)
{
     $responseHeader = get_headers($url);
     return substr($responseHeader[0], 9, 3);
}

$statusCode = get_response_code('http://example.com');
echo $statusCode;
?>

Recommended Read: 10 Amazing CSS Drop Cap Effects you can Copy Paste

Recommended Read: Create a stylish Horizontal Menu Bar with HTML and CSS

I hope you find this php tutorial useful.

Last Modified: Apr-15-2015

1 comment:

Contact Form

Name

Email *

Message *