Чтобы проверить favicon с помощью Google API
Как мы можем проверить, что favicon, предоставленный Google API, является глобусом по умолчанию?
https://www.google.com/s2/u/0/favicons?domain=facebook.com возвращает значок facebook
где https://www.google.com/s2/u/0/favicons?domain=test.com возвращает глобус в качестве значка.
Как мы можем проверить, является ли значок глобусом по умолчанию или нет?
2 ответа
Некоторое время назад я сделал функцию проверки того, возвращается ли значок глобуса по умолчанию.
function getFavicon($domain) {
$data = file_get_contents('https://plus.google.com/_/favicon?domain=' . $domain);
$base64 = base64_encode($data);
return ($data && hash('md5', $base64) !== '99fd8ddc4311471625e5756986002b6b' ? 'data:image/png;base64,' . $base64 : false);
}
$favicon = getFavicon('facebook.com');
if ( $favicon ) {
echo '<img src="' . $favicon . '" />';
}
Когда я натолкнулся на эту же проблему, я выполнил контрольную сумму md5 для возвращенного изображения, сделал заметку о том, каким был хэш md5 для изображения глобуса по умолчанию, и использовал это для подтверждения наличия значка (или нет).
Смотрите пример php кода ниже
<?php
class favicon {
/*
Example Return
Array
(
[md5] => 598900b91902a2de1e8be0a888a3f7bb
[png_string] => // image as a string for saving to file later
[image] => // Embedded Image <img alt="Embedded Image" src="data:image/png;base64,iVBORw0K..." />
[error] => // true or flase
[error_text] => // description of the error
[domain] => // http://www.orange.co.uk/
[endpoint] => https://plus.google.com/_/favicon?domain=http://www.orange.co.uk/
[endpoint_id] => 1
[headers] => Array
(
[Content-Type] => image/png
[X-UA-Compatible] => IE=edge, chrome=1
[Expires] => Mon, 07 Oct 2013 10:39:34 GMT
[Date] => Sun, 06 Oct 2013 10:39:34 GMT
[X-Content-Type-Options] => nosniff
[X-Frame-Options] => SAMEORIGIN
[X-XSS-Protection] => 1; mode=block
[Server] => GSE
[Cache-Control] => public, max-age=86400
[Content-Length] => 232
[Age] => 23
)
)
*/
public static function stream($domain, $endpoint = 1) {
if ($endpoint == 1) {
// endpoint 1
$url = 'https://plus.google.com/_/favicon?domain=' . $domain;
$error = 'b8a0bf372c762e966cc99ede8682bc71'; // md5 of blank image
}
elseif ($endpoint == 2) {
// endpoint 2 etc
$url = 'https://plus.google.com/_/favicon?domain=' . $domain;
$error = 'b8a0bf372c762e966cc99ede8682bc71'; // md5 of blank image
}
else {
return array(
'md5' => null,
'png_string' => '',
'error' => true,
'error_text' => '$endpoint argument should have the value 1 or 2',
'headers' => null,
'domain' => $domain,
'endpoint' => $url,
'endpoint_id' => (int) $endpoint
);
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); // The number of seconds to wait while trying to connect. Use 0 to wait indefinitely.
curl_setopt($ch, CURLOPT_TIMEOUT, 10); // The maximum number of seconds to allow cURL functions to execute.
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); // don’t try and do any clever ssl
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
$part = explode("\r\n\r\n", curl_exec($ch));
curl_close($ch);
$headers = self::http_parse_headers($part[0]);
$md5 = md5($part[1]);
if ($md5 == $error) {
return array(
'md5' => $md5,
'png_string' => '',
'image' => '',
'error' => true,
'error_text' => 'no favicon could be found for this domain',
'domain' => $domain,
'endpoint' => $url,
'endpoint_id' => (int) $endpoint,
'headers' => $headers
);
}
return array(
'md5' => $md5,
'png_string' => $part[1],
'image' => '<img alt="Embedded Image" src="data:image/png;base64,' . base64_encode($part[1]) . '" />',
'error' => false,
'error_text' => null,
'domain' => $domain,
'endpoint' => $url,
'endpoint_id' => (int) $endpoint,
'headers' => $headers
);
}
private static function http_parse_headers($raw_headers) {
if (! empty($raw_headers)) {
$headers = array();
foreach (explode("\n", $raw_headers) as $i => $h) {
$h = explode(':', $h, 2);
if (isset($h[1])) {
$headers[$h[0]] = trim($h[1]);
}
}
return $headers;
}
return false;
}
}
?>