Today, by the wind to share a PHP to get access to the page HTTP state of the encapsulation function;
Here is the key part of the code:
/**
* Get HTTP status of remote URL
*
* @version 0.0.1
* @Author CHENJL *
* @param string $url remote URL
* @param string $data ture[returns an array of HTTP states] | false[return status value]
*
* @return Mixed
*/
function Getheaders ($url, $data =false) {
$_headers = Get_headers ($url, 1);
if (! $data) {return $_headers;}
$curl = Curl_init ();
curl_setopt ($curl, Curlopt_url, $url);//Get content URL
curl_setopt ($curl, curlopt_header,1);//Get HTTP header information
curl_setopt ($curl, curlopt_nobody,1);//Do not return the body information of HTML
curl_setopt ($curl, curlopt_returntransfer,1);//return data stream, not directly output
curl_setopt ($curl, curlopt_timeout,30); Timeout length, Unit seconds
Curl_exec ($curl);
$rtn = Curl_getinfo ($curl, Curlinfo_http_code);
Curl_close ($curl);
return $RTN;
}
The above code, by the wind has been two ways to obtain a consolidation of access, to facilitate the different access to the HTTP status scenario needs;
return Result:
# Invoke Case 1:getheaders (' http://www.111cn.net\/\ ', true);
# return Result: 200//Direct return HTTP status code
# Invoke Case 2:getheaders (' http://www.111cn.net/', false);
# return Result:
/*
Array (10) {
[0]=>
String ("http/1.1 OK")
["Server"]=>
String (5) "Nginx"
["Date"]=>
String "Mon, June 2016 06:21:35 GMT"
["Content-type"]=>
String (9) "text/html"
["Content-length"]=>
String (5) "26898"
["Last-modified"]=>
String "Mon, June 2016 06:16:00 GMT"
["Connection"]=>
String (5) "Close"
["Vary"]=>
String (accept-encoding)
["ETag"]=>
String "" 5779ff20-6912 ""
["Accept-ranges"]=>
String (5) "bytes"
}
*/