file_exists、@get_headers、cURL 和检测图标

file_exists, @get_headers, cURL and detecting a favicon

在各个地方阅读后,包括 http://php.net/manual/en/function.file-exists.php, I have the following working code which detects whether a given host eg. http://example.com 的网站图标位于 /favicon.ico

$file = 'http://www.easyjet.com/favicon.ico';
$file_headers = @get_headers($file);

if($file_headers[0] == 'HTTP/1.0 200 OK') {

    //place favicon as an image on the page
    echo "<img src ='" . $file . "'>";
    } else {
    //place default image
    echo  "<img src ='" . "globe.jpg" . "'>";

}

虽然我知道有更好、更彻底的方法可以在 Whosebug 上查找网站图标文件,但我更关心我对 @get_headers 的使用。我读到的一些内容表明 cURL 可能有用,或者我应该设置一个用户代理。

我是否会 运行 在某些我没有预见到的情况下使用此代码遇到困难?

我个人认为 get_headers 是这种情况下的最佳方式。首先,它执行 HEAD 请求,这对您和远程主机的负载都较小(因为远程主机只提供 headers 而您的服务器不需要自己获取图像)。

我真的不认为这会造成任何麻烦,而且我还没有看到使用该功能引起的任何问题。