xRobot
xRobot

Reputation: 26567

How to get favicon's URL from a generic webpage?

I need a way to get the favicon's URL from a generic webpage considering that the favicon is not always at the base url.

P.s. without using an external service.

Upvotes: 2

Views: 6283

Answers (5)

Emad Saeed
Emad Saeed

Reputation: 116

Use the S2 service provided by google. It is as simple as this

http://www.google.com/s2/favicons?domain=www.yourdomain.com

Upvotes: 1

chepe263
chepe263

Reputation: 2812

For future use, to get the favicon of a page using jQuery you can use

jQuery('link[rel*=icon]').attr('href');

Upvotes: -2

Jeff Lambert
Jeff Lambert

Reputation: 24661

You can use Simple HTML DOM Parser to both get the contents and parse the results:

$html = file_get_html('http://www.google.com/');

$icon = '';
foreach($html->find('link') as $element) {
    if($element->rel == "shortcut icon" || $element->rel == "icon")
        $icon = $element->href;
}

Note: The code above only gets icons if they are specified in a link element

Upvotes: 3

Surreal Dreams
Surreal Dreams

Reputation: 26380

You could use an HTML parser to look for link tags that include favicon information. The type attribute should be set as "image/x-icon" and the rel attribute is either "shortcut icon" or "icon". The href attribute will be the address of the favicon.

Upvotes: 1

Wander Nauta
Wander Nauta

Reputation: 19615

A page's favicon is always either at

Try a HEAD request for /favicon.ico first (CURL should be able to do that), if that doesn't work, fetch the page itself, parse the HTML and see if you can find a matching tag.

Upvotes: 7

Related Questions