2010-01-02 26 views
8

Quiero enumerar sitios web destacados en mi sitio web y pensé que sería genial para honrar y usar su favicon. ¿Cómo lo obtengo del dominio para una URL arbitraria en JSP o XSLT? Puedo disparar PHP o Javascript, pero XSLT es la metodología preferida.¿Cómo puedo recuperar el favicon de un sitio web?

+2

para obtener un favicon uno puede usar esto: http://www.google.com/s2/favicons?domain=domain_name –

Respuesta

17

Para obtener el favicon de una página web, es necesario cargar el código HTML índice de cada sitio web que aparece y comprobar si alguno de los siguientes:

HTML:

<link rel="icon" type="image/vnd.microsoft.icon" href="http://example.com/image.ico"> 
<link rel="icon" type="image/png" href="http://example.com/image.png"> 
<link rel="icon" type="image/gif" href="http://example.com/image.gif"> 

XHTML:

<link rel="icon" type="image/vnd.microsoft.icon" href="/somepath/image.ico" /> 
<link rel="icon" type="image/png" href="/somepath/image.png" /> 
<link rel="icon" type="image/gif" href="/somepath/image.gif" /> 

Internet Explorer puede usar un formato ligeramente diferente:

<link rel="SHORTCUT ICON" href="http://www.example.com/myicon.ico" /> 

También tenga en cuenta que como la mayoría de los navegadores web no requieren el enlace HTML para recuperar un favicon, también debe verificar favicon.ico en la raíz del documento del sitio web, si no se encuentra ninguna de las referencias de enlace anteriores.

Con PHP, es fácil de obtener el contenido HTML de una página web mediante el uso file_get_contents($url):

$url = 'http://www.exmaple.com'; 
$output = file_get_contents($url); 
+2

excelente! Gracias por el detalle Daniel. Revisaré el tutorial de PHP y le contaré cómo funciona. – mobibob

0

Aquí está mi intento de eso. Utiliza diversas estrategias para evitar los muchos casos posibles:

<? 
/* 
    nws-favicon : Get site's favicon using various strategies 

    This script is part of NWS 
    https://github.com/xaccrocheur/nws/ 

*/ 


function CheckImageExists($imgUrl) { 
    if (@GetImageSize($imgUrl)) { 
     return true; 
    } else { 
     return false; 
    }; 
}; 

function getFavicon ($url) { 

$fallback_favicon = "/var/www/favicon.ico";  
// $fallback_favicon = "http://stackoverflow.com/favicon.ico"; 


    $dom = new DOMDocument(); 
    @$dom->loadHTML($url); 
    $links = $dom->getElementsByTagName('link'); 
    $l = $links->length; 
    $favicon = "/favicon.ico"; 
    for($i=0; $i<$l; $i++) { 
     $item = $links->item($i); 
     if(strcasecmp($item->getAttribute("rel"),"shortcut icon") === 0) { 
      $favicon = $item->getAttribute("href"); 
      break; 
     } 
    } 

    $u = parse_url($url); 

    $subs = explode('.', $u['host']); 
    $domain = $subs[count($subs) -2].'.'.$subs[count($subs) -1]; 

    $file = "http://".$domain."/favicon.ico"; 
    $file_headers = @get_headers($file); 

    if($file_headers[0] == 'HTTP/1.1 404 Not Found' || $file_headers[0] == 'HTTP/1.1 404 NOT FOUND' || $file_headers[0] == 'HTTP/1.1 301 Moved Permanently') { 

     $fileContent = @file_get_contents("http://".$domain); 

     $dom = @DOMDocument::loadHTML($fileContent); 
     $xpath = new DOMXpath($dom); 

     $elements = $xpath->query("head/link//@href"); 

     $hrefs = array(); 

     foreach ($elements as $link) { 
      $hrefs[] = $link->value; 
     } 

     $found_favicon = array(); 
     foreach ($hrefs as $key => $value) { 
      if(substr_count($value, 'favicon.ico') > 0) { 
       $found_favicon[] = $value; 
       $icon_key = $key; 
      } 
     } 

     $found_http = array(); 
     foreach ($found_favicon as $key => $value) { 
      if(substr_count($value, 'http') > 0) { 
       $found_http[] = $value; 
       $favicon = $hrefs[$icon_key]; 
       $method = "xpath"; 
      } else { 
       $favicon = $domain.$hrefs[$icon_key]; 
       if (substr($favicon, 0, 4) != 'http') { 
        $favicon = 'http://' . $favicon; 
        $method = "xpath+http"; 
       } 
      } 
     } 

     if (isset($favicon)) { 
      if (!CheckImageExists($favicon)) { 
       $favicon = $fallback_favicon; 
       $method = "fallback"; 
      } 
     } else { 
      $favicon = $fallback_favicon; 
      $method = "fallback"; 
     } 

    } else { 
     $favicon = $file; 
     $method = "classic"; 

     if (!CheckImageExists($file)) { 
      $favicon = $fallback_favicon; 
      $method = "fallback"; 
     } 

    } 
    return $favicon; 
} 

?> 
0

Abra el código fuente de la página (haga clic derecho en Ver el origen de la página) encuentre la línea a continuación, haga clic en el enlace images/favicon.png.

<link rel="icon" href="images/favicon.png" type="image/png" sizes="16x16"> 
Cuestiones relacionadas