-
- function get_http_response_code($theURL) {
- $headers = get_headers($theURL);
- return substr($headers[0], 9, 3);
- }
- ?>
-
Copy code
The function of get_headers is to access a remote address and return the HTTP headers sent by the server in the form of an array. And $header[0] is the status code returned by the server (if nothing else happens, the status code should be the first one returned).
Exclusion example:
-
-
/** - * Fetches all the real headers sent by the server in response to a HTTP request without redirects
- * Gets the headers without redirects
- */
- function get_real_headers($url,$format=0,$follow_redirect=0) {
- if (!$follow_redirect) {
- //set new default options
- $opts = array('http' =>
- array('max_redirects'=>1,'ignore_errors'=>1)
- );
- stream_context_get_default($opts);
- }
//get headers
- $headers=get_headers($url,$format);
- //restore default options
- if (isset($opts)) {
- $opts = array(' http' =>
- array('max_redirects'=>20,'ignore_errors'=>0)
- );
stream_context_get_default($opts);
- }
//return
- return $headers;
- }
- ?>
-
Copy code
|