Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/265.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 当get_headers()尝试解析无法解析的主机时,如何捕获错误?_Php_Header_Http Headers - Fatal编程技术网

Php 当get_headers()尝试解析无法解析的主机时,如何捕获错误?

Php 当get_headers()尝试解析无法解析的主机时,如何捕获错误?,php,header,http-headers,Php,Header,Http Headers,考虑以下几点: $url = 'http://psyng.com/u/9716602b'; $headers = get_headers($url, 1); print_r($headers); 由于域psyng.com无法解析,此代码导致: Warning: get_headers(): php_network_getaddresses: getaddrinfo failed: No such host is known 然后脚本停止运行。有没有一种方法可以让脚本的其余部分继续运行——换

考虑以下几点:

$url = 'http://psyng.com/u/9716602b';
$headers = get_headers($url, 1);
print_r($headers);
由于域psyng.com无法解析,此代码导致:

Warning: get_headers(): php_network_getaddresses: getaddrinfo failed: 
No such host is known
然后脚本停止运行。有没有一种方法可以让脚本的其余部分继续运行——换句话说:捕获错误并继续解析下一个URL?比如:

$url = 'http://psyng.com/u/9716602b';
$headers = get_headers($url, 1);
if ($headers == 'No such host is known') {
  // nevermind, just move on to the next URL in the list...
}
else {
  // resolve header stuff...
}

脚本不应该停止运行,因为生成的消息只是一个警告。我自己测试了这个脚本,这就是我看到的行为。您可以从中看到,
get_headers()
在失败时将返回
FALSE
,因此您的情况实际上应该是

if ($headers === FALSE) {
    // nevermind, just move on to the next URL in the list...

函数get_headers返回一个布尔结果;print_r的目的是以人类可读的格式返回布尔值

<?php
$url = 'http://psyng.com/u/9716602b';
$headers = get_headers($url, 1);
if ($headers === FALSE) { //Test for a boolean result.
  // nevermind, just move on to the next URL in the list...
}
else {
  // resolve header stuff...
}
?>

get\u头不一致: