php怎么判断http状态
时间:2021-10-02 来源:互联网
今天PHP爱好者为您带来php判断http状态的方法:【header("HTTP/1.1 404 Not Found"); $url="http://www.xxxx.com/preg.php"; $ch = curl_init(); curl_seto...】。希望对大家有所帮助。
本文操作环境:windows10系统、php 7、thinkpad t480电脑。
我们在实际的项目开发中有时会需要知道远程的URL地址能否正常访问,通过判断其正常与否来决定是否进行进行下一步的操作。那么我们该如何判断HTTP的状态呢?其实并不难,我们可以采取如下两种方式,接下来就让我们一起来看看这两种方式吧。
文件preg.php
header("HTTP/1.1 404 Not Found");
第一种方法:
$url = "http://www.demo.com/preg.php";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_NOBODY, TRUE); // remove body
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$head = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
echo $httpCode;
curl_close($ch);
运行结果:
404
第二种方法:
echo '<pre>';
print_r(get_headers("http://www.demo.com/preg.php",1));
运行结果:
Array
(
[0] => HTTP/1.1 404 Not Found
[Date] => Fri, 28 Sep 2018 09:27:03 GMT
[Server] => Apache/2.4.23 (Win32) OpenSSL/1.0.2j PHP/5.5.38
[X-Powered-By] => PHP/5.5.38
[Content-Length] => 0
[Content-Type] => text/html
)
以上就是php怎么判断http状态的详细内容,更多请关注php爱好者其它相关文章!
-
原神5.7版本卡池角色-新版卡池角色前瞻 2025-06-03
-
原神5.7版本什么时候上线-5.7版本开始时间 2025-06-03
-
币安怎么购买ALE币?ALE购买教程与币安binance下载入口 2025-06-03
-
燕云手机不兼容怎么办-手机不兼容燕云解决方案 2025-06-03
-
崩坏星铁谎言新角色光锥谎言在风中飘扬上线时间 2025-06-03
-
崩坏星铁谎言在风中飘扬光锥怎么获得-抽取规则 2025-06-03