2

次の点を考慮してください。

$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...
}
4

2 に答える 2

3

生成されたメッセージは単なる警告であるため、スクリプトの実行を停止しないでください。私はこのスクリプトを自分でテストしましたが、それが私が見た動作です。失敗時に返されるドキュメントで確認できるので、実際の状態はget_headers()FALSE

if ($headers === FALSE) {
    // nevermind, just move on to the next URL in the list...
于 2012-04-29T13:23:30.293 に答える
0

関数 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...
}
?>
于 2012-04-29T14:00:43.223 に答える