4

我们如何检查 Google API 提供的 favicon 是否为默认地球仪?

https://www.google.com/s2/u/0/favicons?domain=facebook.com返回 的 favicon facebook,其中https://www.google.com/s2/u/0/favicons?domain =test.com将地球作为网站图标返回。

我们如何检查网站图标是否是默认地球仪?

4

2 回答 2

6

不久前我做了一个函数来检查是否返回了默认的地球图标。

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 . '" />';
}
于 2015-01-26T11:02:46.603 回答
0

当我遇到同样的问题时,我对返回的图像执行了 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;
    }
}
?>
于 2013-11-13T05:14:42.130 回答