考虑以下:
$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...
}