Home > Backend Development > PHP Problem > php gets website ico

php gets website ico

(*-*)浩
Release: 2023-02-27 09:28:01
Original
2923 people have browsed it

php gets website ico

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[&#39;url&#39;])){
	    $icon = file_get_contents("http://www.google.com/s2/favicons?domain=" . $_GET[&#39;url&#39;]);
	    if($icon){
	        header(&#39;Content-type:image/png&#39;);
	        echo $icon;
	    }
Copy after login

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!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template