PHP Get access page HTTP status Code _php technique

Source: Internet
Author: User

No more nonsense. Directly on the code

Core code:

/**
 * Get HTTP status of remote URL
 * * 
 @version 0.0.1
 * @Author CHENJL * * * 
 @param string $url    remote URL
 * @ param string $data   ture[return HTTP status array] | 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 without direct 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.jb51.net\/\ ', true);
# return Result: 200//Direct return HTTP status Code
 
# call Case 2:getheaders (' http://www.jb51.net/', false);
# return Result:/
*
Array {  
[0]=>  
string ' http/1.1 ok '  
[' Server ']=>  
string (5) ' Nginx "  
[Date"]=>  
string "Mon, 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 (15)] 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.