Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP get_headers() alternative

I need a PHP script that reads the HTTP response code for each URL request.

something like

$headers = get_headers($theURL);
return substr($headers[0], 9, 3);

The problem is the get_headers() function is disabled at server level, as a policy.So it doesn't work.

The question is how to get the HTTP response code for a URL?

like image 306
new_b Avatar asked May 22 '11 10:05

new_b


People also ask

How can I get http header in PHP?

The header() function in PHP sends a raw HTTP header to a client or browser. Before HTML, XML, JSON, or other output is given to a browser or client, the server sends raw data as header information with the request (particularly HTTP Request).

How do I view headers in PHP?

PHP | get_headers() Function The get_headers() function in PHP is used to fetch all the headers sent by the server in the response of an HTTP request. Parameters: This function accepts three parameters as mentioned above and described below: $url: It is a mandatory parameter of type string. It defines the target URL.


3 Answers

If cURL is enabled, you can use it to get the whole header or just the response code. The following code assigns the response code to the $response_code variable:

$curl = curl_init();
curl_setopt_array( $curl, array(
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_URL => 'http://stackoverflow.com' ) );
curl_exec( $curl );
$response_code = curl_getinfo( $curl, CURLINFO_HTTP_CODE );
curl_close( $curl );

To get the whole header you can issue a HEAD request, like this:

$curl = curl_init();
curl_setopt_array( $curl, array(
    CURLOPT_HEADER => true,
    CURLOPT_NOBODY => true,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_URL => 'http://stackoverflow.com' ) );
$headers = explode( "\n", curl_exec( $curl ) );
curl_close( $curl );
like image 56
Matěj G. Avatar answered Sep 22 '22 03:09

Matěj G.


Use HttpRequest if you can: http://de2.php.net/manual/en/class.httprequest.php

$request = new HttpRequest("http://www.example.com/");
$request->send();
echo $request->getResponseCode();

Or do it the hard way: http://de2.php.net/manual/en/function.fsockopen.php

$errno = 0;
$errstr = "";

$res = fsockopen('www.example.com', 80, $errno, $errstr);

$request = "GET / HTTP/1.1\r\n";
$request .= "Host: www.example.com\r\n";
$request .= "Connection: Close\r\n\r\n";

fwrite($res, $request);

$head = "";

while(!feof($res)) {
    $head .= fgets($res);
}

$firstLine = reset(explode("\n", $head));
$matches = array();
preg_match("/[0-9]{3}/", $firstLine, $matches);
var_dump($matches[0]);

Curl may be also a good option, but the best option is to beat your admin ;)

like image 38
reeaal Avatar answered Sep 23 '22 03:09

reeaal


You can build and read your own HTTP queries with fsockopen and regular file operations. Check out my earlier answer on this topic:

Are there any other options for rest clients besides CURL?

like image 28
Emil Vikström Avatar answered Sep 23 '22 03:09

Emil Vikström