PHP gets HTTP header response code without cURL

I wrote a class that determines if cURL is available if it does GET, POST, DELETE using cURL. In the cURL version, I use curl_getinfo($curl, CURLINFO_HTTP_CODE);to get the HTTP code. If cURL is not available, it uses fopen () to read the contents of the file. How to get HTTP header code without cURL?

+5
source share
2 answers

Whenever you perform any kind of interaction with HTTP, a special variable $http_response_headerin the same area will contain all the headers (including the status bar header) that are the result of the last HTTP interaction.

See here for an example of how to parse it and get a status code .

+6

get_headers:

<?php
$url = "http://www.example.com/";

$headers = get_headers($url);

$code = $headers[0];
?>

: get_headers . $http_response_headers hakre.

+14

All Articles