2010-01-02 121 views
8

我想在我的网站上列出特色网站,我认为这将是很酷的兑现和使用他们的图标。在JSP或XSLT中,如何从域中获取任意URL?我可以发布PHP或JavaScript,但XSLT是首选的方法。如何检索网站的图标?

+2

得到一个favicon可以使用这个:http://www.google.com/s2/favicons?domain=domain_name – 2017-06-14 14:59:49

回答

17

为了得到一个网站的图标,你需要加载每个功能网站的索引HTML,检查以下任一操作:

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可能会使用稍微不同的格式:

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

另请注意,由于大多数Web浏览器不需要HTML链接来检索收藏夹图标,因此如果找不到以上任何链接引用,则还应该在网站的文档根目录中检查favicon.ico

使用PHP,很容易通过使用file_get_contents($url)得到一个网页的HTML内容:

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

优秀!感谢Daniel的细节。我将检查PHP教程,并让你知道它是如何工作的。 – mobibob 2010-01-02 05:02:45

0

这是我在尝试它。它使用各种策略来解决许多可能的情况:

<? 
/* 
    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

打开页面源代码(右键单击查看页面源代码)找到下面提到的行,单击images/favicon.png链接。

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