How to get favicon default icons for different sites

Source: Internet
Author: User

Reference article: 1, the use of public APIs to extract any site Favicon.ico icon how to read Favicon according to the settings favicon, there are 2 ways to read favicon:  a, the default directly read the root directory favicon.ico files. B, if there is no Favicon.ico file in the root directory, read the Favicon declaration on the page. In contrast, getting the Favicon.ico file under the root of the Web site is the easiest and quickest, but if the site root does not have the file, you need to use the background program to read the source code of the Web page, very troublesome.    in order to overcome the trouble of acquiring Favicon, there are some public APIs to get favicon, such as:  dnspod: Favicon?domain=url website "Special Recommendation" google: Domain name getfavicon:http:// address address for network speed, Dnspod is domestic, fast and stable. Google's stability is trustworthy, but because it is often outside the wall, it has to give up. The 3rd Getfavicon is the site of the early acquisition of Favicon, which has now been game over. The 4th one is also foreign, and is often inaccessible in some areas. The 1th and 2nd wins.   for passing parameters, Dnspod and Google are both the name of the parameter, and the 3rd and 4th are the URL of the parameter. The 3rd and 4th wins.   Overall, 4 of them were tied.   In this situation, I developed a public API to get the site Favicon, just to get pictures in the incoming URL, the current server set up in Hong Kong, borderless access, you can get any URL favicon. Also, the picture has a 30-day cache period, and the 2nd time to get the same domain name (including multi-level domain names) is favicon faster. The use of the method is also very simple, such as:  get Baidu favicon http://*/?url=  get Google favicon http://*/?url=  get Facebook's Favicon http://*/?url=  get GitHub's favicon http://*/?url=http:// due to traffic and abuse, has now jumped to the Dnspod server favicon access, please use the API's friends to migrate as soon as possible, the API will be completely closed by the end of this year. Please use the Http:// website address to obtain the favicon of the site.  2, how to get site Favicon using PHP

Recently do a tab need to display the site's Favicon next to the site name to improve the display effect,

Start to think is to use Google's way to obtain, using the " url" way can directly obtain the site's favicon icon and to 16* 16 the size of the picture display, this method is simple and convenient, but in some network environment but there will be pictures can not display the problem (need FQ), in order to solve this bug I decided to write a function to get favicon, use their own server to avoid FQ.

See examples for actual effects:


If you do not want to write your own method can also use the interface I provided, that is, "Http:// url", the URL can be prefixed with http://.

Code (the way you call Google, which reduces the amount of code and is faster):

1 <?php
2 if(isset($_GET[‘url‘])){
3     $iconfile_get_contents(""$_GET[‘url‘]);
4     if($icon){
5         header(‘Content-type:image/png‘);
6         echo$icon;
7     }
8 }

Yes, just a few lines of code to fix everything ^_^

So as long as we use the server can access Google can be normal display favicon, no longer affected by the network environment.

More complex method is to write their own functions, here I only provide ideas, no longer write code, if there is a need for the code to leave a message, set attached.

The general website will have its own favicon icon named "Favicon.ico" and placed in the root directory of the site, such as Http:// So you can directly use PHP function file_get_contents () to get the picture content, set the header as a PNG image, display it.

If the root directory does not favicon.ico this file, you can use file_get_contents or curl to get the contents of the Web page, using the regular find "<link rel=" shortcut icon "href=", "/>", In the href is the Favicon file location, directly to get its content.

How to get favicon default icons for different sites

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.