Depending on how the favicon is set, there are two ways to read the favicon:
A. By default, the website is read directly. The favicon.ico file in the root directory. (Recommended learning: PHP video tutorial)
B. If the favicon.ico file in the root directory does not exist, read the favicon statement in the page.
In contrast, it is the simplest and fastest to obtain the favicon.ico file in the root directory of the website. However, if there is no such file in the root directory of the website, you need to use a background program to read the source code of the web page, which is very Trouble.
How to get the website Favicon using PHP
Recently when making a Tab, it is necessary to display the website's Favicon next to the website name to improve the display effect, as shown in the figure:
icetab When I started doing it, I thought of using Google to get it. Using "http://www.google.com/s2/favicons?domain=URL", you can directly get the website's Favicon icon and display it as 16* The image is displayed in the form of a 16-size image. This method is simple and convenient, but in some network environments there will be a problem that the image cannot be displayed (need to circumvent the wall). In order to solve this bug, I decided to rewrite a function to get the Favicon and use my own Server to avoid circumvention.
Please see the example for the actual effect:
http://favicon.byi.pw/?url=blog.icewingcc.com
If you don’t want to write the method yourself, you can Use the interface I provided, that is, "http://favicon.byi.pw/?url=URL", and the URL can be prefixed with http://.
Code (the way to call Google, this method can reduce the amount of code and is faster):
<?php if(isset($_GET['url'])){ $icon = file_get_contents("http://www.google.com/s2/favicons?domain=" . $_GET['url']); if($icon){ header('Content-type:image/png'); echo $icon; }
Yes, just these few lines of code can do everything^_^
In this way, as long as the server we use can access Google, the Favicon can be displayed normally and is no longer affected by the network environment.
The above is the detailed content of php gets website ico. For more information, please follow other related articles on the PHP Chinese website!