How to get the header of a web page in PHP

3 Answers

0 votes
function get_header($url) {
	$handle = curl_init();
        
	curl_setopt($handle, CURLOPT_HTTPGET, true);
	curl_setopt($handle, CURLOPT_HEADER, true);
	curl_setopt($handle, CURLOPT_URL, $url);
	curl_setopt($handle, CURLOPT_FOLLOWLOCATION, true);
	curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
        
	$output = curl_exec($handle);

	curl_close($handle);
        
	$separator = "\r\n\r\n";
	$header = substr($output, 0, strpos($output, $separator));
	
	return $header;
}


$url = "https://seek4info.com";

$header = get_header($url);

$header_array = Array();

foreach (explode("\r\n", $header) as $count => $line) {
    if ($count === 0) {
        $header_array['http_code'] = $line;
        $status_info = explode(" ", $line);
        $header_array['status_info'] = $status_info;
    } else {
            list ($key, $value) = explode(': ', $line);
                  $header_array[$key] = $value;
    }
}

echo "<pre>";
print_r($header_array);
echo "<pre />";


/*
run:

Array
(
    [http_code] => HTTP/1.1 200 OK
    [status_info] => Array
        (
            [0] => HTTP/1.1
            [1] => 200
            [2] => OK
        )

    [Connection] => close
    [X-Powered-By] => PHP/5.4.45
    [Set-Cookie] => PHPSESSID=6q6glnoe47lg7aoqqca60lpnc5; path=/
    [Expires] => Thu, 19 Nov 1981 08:52:00 GMT
    [Cache-Control] => no-store, no-cache, must-revalidate, post-check=0, pre-check=0
    [Pragma] => no-cache
    [Content-Type] => text/html
    [Transfer-Encoding] => chunked
    [Date] => Thu, 12 Sep 2019 18:17:27 GMT
    [Server] => LiteSpeed
)

*/

 



answered Sep 12, 2019 by avibootz
edited Sep 12, 2019 by avibootz
0 votes
$url = "https://seek4info.com";

$header = get_headers($url);

echo "<pre>";
print_r($header);
echo "<pre />";


/*
run:

Array
(
    [0] => HTTP/1.0 200 OK
    [1] => Connection: close
    [2] => X-Powered-By: PHP/5.4.45
    [3] => Set-Cookie: PHPSESSID=3cvbaniu8b7n3allff904e9gf7; path=/
    [4] => Expires: Thu, 19 Nov 1981 08:52:00 GMT
    [5] => Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
    [6] => Pragma: no-cache
    [7] => Content-Type: text/html
    [8] => Date: Fri, 13 Sep 2019 03:12:57 GMT
    [9] => Server: LiteSpeed
)

*/

 



answered Sep 13, 2019 by avibootz
edited Sep 13, 2019 by avibootz
0 votes
$url = "https://seek4info.com";
 
$header = get_headers($url, 1);
 
echo "<pre>";
print_r($header);
echo "<pre />";
 
 
/*
run:
 
Array
(
    [0] => HTTP/1.0 200 OK
    [Connection] => close
    [X-Powered-By] => PHP/5.4.45
    [Set-Cookie] => PHPSESSID=ulqu962crui3neubrcauo0r6i1; path=/
    [Expires] => Thu, 19 Nov 1981 08:52:00 GMT
    [Cache-Control] => no-store, no-cache, must-revalidate, post-check=0, pre-check=0
    [Pragma] => no-cache
    [Content-Type] => text/html
    [Date] => Fri, 13 Sep 2019 13:51:03 GMT
    [Server] => LiteSpeed
)
 
*/

 



answered Sep 13, 2019 by avibootz

Related questions

1 answer 188 views
2 answers 246 views
1 answer 260 views
1 answer 178 views
178 views asked Sep 13, 2019 by avibootz
3 answers 376 views
1 answer 174 views
...