PHP get_headers()结果区分

时间:2019-01-16 00:12:12

标签: php error-checking get-headers

我使用函数get_headers($ url)。

如果根据http://php.net/manual/en/language.types.boolean.php,e,pty字符串等于false。

如何区分返回get_headers($ url)=== false和空字符串的函数?

或者换句话说,如何区分错误情况和良好情况?

谢谢

1 个答案:

答案 0 :(得分:0)

如果要检查HTTP状态,请使用以下命令:

<?php
$headers = get_headers($url);
if($headers !== FALSE AND $headers[0] === 'HTTP/1.0 200 OK'){
    print_r($headers);
}else{
    echo 'Error';
}

检查任何HTTP状态:

<?php
$headers = get_headers($url);
if($headers !== FALSE){ // or can use "is_array($headers)"
    print_r($headers);
}else{
    echo 'Error';
}