PHP Get access page HTTP status code example

Source: Internet
Author: User

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"
}
*/

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.