PHP implementation code to obtain the HTTP status code of the access page
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; }
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" } */
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

WebStorm Mac version
Useful JavaScript development tools
