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爱好者其它相关文章!
-
DOGE币流通市值和持币地址数统计 2025-06-24
-
树懒:快节奏生活中的行为艺术家。 2025-06-24
-
DOGE币合约杠杆倍数及支持合约的平台 2025-06-24
-
猪:没想到吧?我智商超高! 2025-06-24
-
LTC币在哪些平台上线?币安和OKX支持情况 2025-06-24
-
LTC币上市时间及首发价格回顾 2025-06-24