2013-08-28 6 views

답변

0

반환 된 이미지의 md5 체크섬을 수행하여 기본 글로브 이미지 md5 해시가 무엇인지 기록하고 favicon의 존재를 확인하는 데 사용되었습니다.

내가 잠시 다시 기본 지구본 아이콘이 반환되는 경우 검사 어떤 함수를 만들어

<?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; 
    } 
} 
?> 
3

아래 예제 PHP 코드를 참조하십시오.

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 . '" />'; 
}