本文操作环境: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 bodycurl_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培训
以上就是php怎么判断http状态的详细内容。
