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爱好者其它相关文章!
-
2025年Web3项目投资指南:SUI、LINK、ENA机会与风险解析 2025-11-08 -
-
2025全球虚拟币交易量排行:BTC、ETH、SOL主流币对比解析 2025-11-08 -
"什么胶囊是什么梗"揭秘:全网爆火的治愈梗,一颗胶囊引爆暖心讨论! 2025-11-08 -
2025年值得关注的新币潜力项目推荐:TAIKO、ZRO、JUP即将上线 2025-11-08 -
2025加密货币周涨幅TOP10:PEPE、SOL、ENA领跑市场最新趋势 2025-11-08