How can I retrieve the favicon of a website?

前端 未结 6 2011
滥情空心
滥情空心 2021-01-30 09:40

I want to list featured websites on my website and I thought it would be cool to honor and use their favicon. How do I get it from the domain for an arbitrary URL in either JSP

6条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-30 10:15

    Here is my attempt at it. It uses various strategies to work around the many possible cases :

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

提交回复
热议问题