Posted By

phpdev on 02/13/13


Tagged

url php image script favicon snippet Website


Versions (?)

Get the Favicon from a website


 / Published in: PHP
 

Example of use:

<a href="http://www.php.net/manual/en/index.php"><img src="<?php echo get_favicon('http://www.php.net/manual/en/index.php') ?>" alt="Favicon" title="See PHP documentation" width="16" height="16" /></a>

  1. /**
  2.  * Get favicon from a URL.
  3.  *
  4.  * @author Pierre-Henry Soria <[email protected]>
  5.  * @copyright (c) 2013, Pierre-Henry Soria. All Rights Reserved.
  6.  * @param string $sUrl
  7.  * @return string The favicon image.
  8.  */
  9. function get_favicon($sUrl)
  10. {
  11. $sApiUrl = 'http://www.google.com/s2/favicons?domain=';
  12. $sDomainName = get_domain($sUrl);
  13.  
  14. return $sApiUrl . $sDomainName;
  15. }
  16.  
  17. /**
  18.  * Get domain name from a URL (helper function).
  19.  *
  20.  * @author Pierre-Henry Soria <[email protected]>
  21.  * @copyright (c) 2013, Pierre-Henry Soria. All Rights Reserved.
  22.  * @param string $sUrl
  23.  * @return string $sUrl Returns the URL to lower case and without the www. if present in the URL.
  24.  */
  25. function get_domain($sUrl)
  26. {
  27. $sUrl = str_ireplace('www.', '', $sUrl);
  28. $sHost = parse_url($sUrl, PHP_URL_HOST);
  29. return $sHost;
  30. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: bellmyer on January 26, 2015

I'd recommend using grabicon.com instead of google's S2 favicon service. Google's service was never meant to be public, and could go away at any time. Grabicon was built to be a drop-in replacement - it's also free, and it works the same way.

It also lets you specify what size you want the icon to be, and generates unique default icons for sites that don't have one. This ensures a uniform user experience for web/mobile apps, because icons are always the requested size, and never missing.

Example:

http://grabicon.com/icon?domain=wikipedia.org

You need to login to post a comment.