问题
How can we check favicon provided by Google API is the default globe?
https://www.google.com/s2/u/0/favicons?domain=facebook.com returns the favicon of the facebook
, where as https://www.google.com/s2/u/0/favicons?domain=test.com returns the globe as the favicon.
How can we check if the favicon is default globe or not?
回答1:
I made a function a while back what checks if the default globe icon is returned.
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 . '" />';
}
回答2:
When I came across this same issue I performed an md5 checksum of the returned image, made a note of what the default globe image md5 hash was and used that to confirm the presence of a favicon (or not).
See example php code below
<?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;
}
}
?>
来源:https://stackoverflow.com/questions/18480250/to-check-favicon-using-google-api