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爱好者其它相关文章!
-
燕云十六声滹沱蹊跷位置一条龙收集 2026-01-15 -
燕云十六声周年庆充值攻略 六元外观买一送一攻略 2026-01-15 -
如鸢1月神秘洞窟攻略 全或半自动无惇无闿通关攻略 2026-01-15 -
明日方舟终末地陈千语背景故事 陈千语剧情介绍 2026-01-15 -
遗弃之地斩三尸宝石怎么选 斩三尸宝石选择推荐 2026-01-15 -
鹅鸭杀怎么报警 报警召集会议方法介绍 2026-01-15