How does Php get only the http status code for accessing the page?

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.