How to get a website's favicon with PHP?

后端 未结 13 1122
醉梦人生
醉梦人生 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:42

    See this answer : https://stackoverflow.com/a/22771267. It's an easy to use PHP class to get the favicon URL and download it, and it also gives you some informations about the favicon like file type or how the favicon was found (default URL, tag...) :

    icoExists){
        echo "Favicon found : ".$favicon->icoUrl."\n";
    
        // Saving favicon to file
        $filename = 'favicon-'.time().'.'.$favicon->icoType;
        file_put_contents($filename, $favicon->icoData);
        echo "Saved to ".$filename."\n\n";
    } else {
        echo "No favicon for ".$favicon->url."\n\n";
    }
    
    $favicon->debug();
    /*
    FaviconDownloader Object
    (
        [url] => https://code.google.com/p/chromium/issues/detail?id=236848
        [pageUrl] => https://code.google.com/p/chromium/issues/detail?id=236848
        [siteUrl] => https://code.google.com/
        [icoUrl] => https://ssl.gstatic.com/codesite/ph/images/phosting.ico
        [icoType] => ico
        [findMethod] => head absolue_full
        [error] => 
        [icoExists] => 1
        [icoMd5] => a6cd47e00e3acbffffd2e8a760dfe64cdc
    )
    */
    ?>
    

提交回复
热议问题