php下获取http状态的实现代码

  在项目开发中,有时我们需要知道远程的URL地址是否能访问正常,判断其正常与否后进行下一步的操作,那么在PHP中如何获取远程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
)

转载于:https://www.cnblogs.com/zhouguowei/p/9719849.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值