In some cases, you often need to obtain the http status code for accessing the page without returning the page content. How can this function be implemented? Refer to the following key code: [php] & lt ;? PhpfunctionGetHttpStatusCode ($ url) {$ curlcurl_init (); cu... Synt
In some cases, you often need to obtain the http status code for accessing the page without returning the page content. How can this function be implemented? Refer to the following key code:
[Php] Function GetHttpStatusCode ($ url ){
$ Curl = curl_init ();
Curl_setopt ($ curl, CURLOPT_URL, $ url); // get the content url
Curl_setopt ($ curl, CURLOPT_HEADER, 1); // get http header information
Curl_setopt ($ curl, CURLOPT_NOBODY, 1); // no html body information is returned.
Curl_setopt ($ curl, CURLOPT_RETURNTRANSFER, 1); // return data stream, not directly output
Curl_setopt ($ curl, CURLOPT_TIMEOUT, 30); // timeout duration, in seconds
Curl_exec ($ curl );
$ Rtn = curl_getinfo ($ curl, CURLINFO_HTTP_CODE );
Curl_close ($ curl );
Return $ rtn;
}
$ Url = "http://www.2cto.com ";
Echo GetHttpStatusCode ($ url );
?>
Function GetHttpStatusCode ($ url ){
$ Curl = curl_init ();
Curl_setopt ($ curl, CURLOPT_URL, $ url); // get the content url
Curl_setopt ($ curl, CURLOPT_HEADER, 1); // get http header information
Curl_setopt ($ curl, CURLOPT_NOBODY, 1); // no html body information is returned.
Curl_setopt ($ curl, CURLOPT_RETURNTRANSFER, 1); // return data stream, not directly output
Curl_setopt ($ curl, CURLOPT_TIMEOUT, 30); // timeout duration, in seconds
Curl_exec ($ curl );
$ Rtn = curl_getinfo ($ curl, CURLINFO_HTTP_CODE );
Curl_close ($ curl );
Return $ rtn;
}
$ Url = "http://www.2cto.com ";
Echo GetHttpStatusCode ($ url );
?>
If Baidu can access it normally, of course the above code runs and the result is 200.
Curl-related detailed usage, see: http://cn2.php.net/manual/zh/ref.curl.php
From the pure soul