Home php教程 PHP开发 PHP implementation code to obtain the HTTP status code of the access page

PHP implementation code to obtain the HTTP status code of the access page

Dec 26, 2016 pm 02:04 PM
http status code

Stop talking nonsense and go straight to the code

Core code:

/**
 * 获取远程URL的HTTP状态
 *
 * @version 0.0.1
 * @Author Chenjl *
 * @param string $url    远程URL
 * @param string $data   ture[返回HTTP状态数组] | false[返回状态数值]
 *
 * @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);//获取内容url
  curl_setopt($curl,CURLOPT_HEADER,1);//获取http头信息
  curl_setopt($curl,CURLOPT_NOBODY,1);//不返回html的body信息
  curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);//返回数据流,不直接输出
  curl_setopt($curl,CURLOPT_TIMEOUT,30); //超时时长,单位秒
  curl_exec($curl);
  $rtn= curl_getinfo($curl,CURLINFO_HTTP_CODE);
  curl_close($curl);
  return $rtn;
}
Copy after login

Zhufeng has two ways to obtain the above code Make an integration to facilitate the needs of different HTTP status acquisition scenarios;

Return results:

# 调用案例1:getHeaders('http://www.php.cn\/\',true);
# 返回结果:200 // 直接返回HTTP状态码
  
# 调用案例2:getHeaders('http://www.php.cn',false);
# 返回结果:
/*
array(10) { 
[0]=> 
string(15) "HTTP/1.1 200 OK" 
["Server"]=> 
string(5) "nginx" 
["Date"]=> 
string(29) "Mon, 04 Jul 2016 06:21:35 GMT" 
["Content-Type"]=> 
string(9) "text/html" 
["Content-Length"]=> 
string(5) "26898" 
["Last-Modified"]=> 
string(29) "Mon, 04 Jul 2016 06:16:00 GMT" 
["Connection"]=> 
string(5) "close" 
["Vary"]=> 
string(15) "Accept-Encoding" 
["ETag"]=> 
string(15) ""5779ff20-6912"" 
["Accept-Ranges"]=> 
string(5) "bytes" 
} 
*/
Copy after login

More PHP obtains the HTTP status code of the access page For articles related to the implementation code, please pay attention to the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Revealing the causes of HTTP status code 460 Revealing the causes of HTTP status code 460 Feb 19, 2024 pm 08:30 PM

Revealing the causes of HTTP status code 460

A deep dive into the meaning and usage of HTTP status code 460 A deep dive into the meaning and usage of HTTP status code 460 Feb 18, 2024 pm 08:29 PM

A deep dive into the meaning and usage of HTTP status code 460

What does http status code 520 mean? What does http status code 520 mean? Oct 13, 2023 pm 03:11 PM

What does http status code 520 mean?

Analyze the causes and solutions of HTTP status code 460 Analyze the causes and solutions of HTTP status code 460 Feb 20, 2024 pm 02:03 PM

Analyze the causes and solutions of HTTP status code 460

Introduction to HTTP 525 status code: explore its definition and application Introduction to HTTP 525 status code: explore its definition and application Feb 18, 2024 pm 10:12 PM

Introduction to HTTP 525 status code: explore its definition and application

An in-depth analysis of HTTP status code 550: Bad email address An in-depth analysis of HTTP status code 550: Bad email address Feb 18, 2024 pm 01:44 PM

An in-depth analysis of HTTP status code 550: Bad email address

Troubleshooting the causes and solutions for HTTP status code 550 Troubleshooting the causes and solutions for HTTP status code 550 Feb 20, 2024 am 09:49 AM

Troubleshooting the causes and solutions for HTTP status code 550

Analysis of the meaning of HTTP status code 460 Analysis of the meaning of HTTP status code 460 Feb 24, 2024 pm 03:51 PM

Analysis of the meaning of HTTP status code 460

See all articles