获得与cURL favicon -我怎么能检查,如果一个favicon实际上存在


getting favicon with cURL - how can I check if a favicon actually exists

我使用Google和cURL的组合来检索和保存我的用户在他们的个人资料中输入的网站的图标。我使用下面的代码:

$url = 'www.madeupwebsitewithnoFavicon.com';
$fp = fopen ('img/favicons/'.$saveFileName.'.png', 'w+');
$ch = curl_init('http://www.google.com/s2/favicons?domain='.$url);
curl_setopt($ch, CURLOPT_TIMEOUT, 6);
/* Save the returned data to a file */
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_exec($ch);
curl_close($ch);
fclose($fp);

我想知道这个网站是否真的有一个图标,这样我就可以继续并将有关它的东西保存到数据库中。我在谷歌上搜索过,但找不到适合我的解决方案。

我确实看到了一些看起来可以为我工作的东西,但是没有:

$pattern = '/<link[^>]+rel's*='s*["'']?'s*shortcut's+icon's*["'']?/i';
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
return preg_match($pattern, curl_exec($ch));

有人有什么想法吗?这对我来说有点陌生

如果卷曲$domain."./favicon.ico"并测试http响应是否为200呢?