How to get a website's favicon with PHP?

后端 未结 13 1135
醉梦人生
醉梦人生 2020-12-02 16:08

I want to get, requested website\'s favicon with PHP. I have been recommended using Google\'s favicon service but it is not functional. I want to do something on my own but

13条回答
  •  误落风尘
    2020-12-02 16:47

    First Method in which we can search it from fevicon.ico if found than it will show it up else not

    ';
            }
            else
            {
                echo "Not found";
            }
        ?>
    

    In other method you can search for icon and get that icon file

        ';
    function getFavicon($site)
    {
                $html=file_get_contents("http://www.".$site);
                $dom=new DOMDocument();
                @$dom->loadHTML($html);
                $links=$dom->getElementsByTagName('link');
                $fevicon='';
    
                for($i=0;$i < $links->length;$i++ )
                {
                    $link=$links->item($i);
                    if($link->getAttribute('rel')=='icon'||$link->getAttribute('rel')=="Shortcut Icon"||$link->getAttribute('rel')=="shortcut icon")
                    {
                        $fevicon=$link->getAttribute('href');
                    }
                }
                return  $fevicon;
    }
    ?>
    

提交回复
热议问题