Google api 使用Google API检查favicon
我们如何检查谷歌API提供的favicon是默认的globe 返回facebook的favicon,其中as返回globe作为faviconGoogle api 使用Google API检查favicon,google-api,favicon,google-ajax-api,Google Api,Favicon,Google Ajax Api,我们如何检查谷歌API提供的favicon是默认的globe 返回facebook的favicon,其中as返回globe作为favicon 我们如何检查favicon是否为默认的globe?当我遇到同样的问题时,我对返回的图像执行了md5校验和,记录了默认的globe图像md5哈希是什么,并用它来确认favicon的存在(或否) 请参见下面的php代码示例 <?php class favicon { /* Example Return Array (
我们如何检查favicon是否为默认的globe?当我遇到同样的问题时,我对返回的图像执行了md5校验和,记录了默认的globe图像md5哈希是什么,并用它来确认favicon的存在(或否) 请参见下面的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;
}
}
?>
我不久前做了一个函数,用于检查是否返回默认的地球仪图标
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 . '" />';
}
函数getFavicon($domain){
$data=文件\u获取\u内容('https://plus.google.com/_/favicon?domain=“.$域);
$base64=base64_编码($data);
return($data&&hash('md5',$base64)!='99fd8ddc4311471625e5756986002b6b'?'data:image/png;base64'。$base64:false);
}
$favicon=getFavicon('facebook.com');
如果($favicon){
回声';
}
我需要用JS做这件事有什么想法吗?我需要用JS做这件事有人想出了解决方案吗?