Jump to content

get favicon


wannabe21

Recommended Posts

You say dependancy cause you are relying on someone elses site to provide content for you?  It's not different than linking to an external script or "add this" button.  Many sites do have their favicons in the main root and is always labeled favicon.ico so you could just do http://domain.com/favicon.ico, otherwise you'd have to do page scraping for the <link rel="favicon">.

Again, what is it you are trying to do with the icon?  Display it on your site, store it in a db, hotlink it?

Link to comment
https://forums.phpfreaks.com/topic/288188-get-favicon/#findComment-1477990
Share on other sites

I put smth together and would appreciate feedback, perhaps it can be faster

 

with the function below you can 'feed' any url, it will check if there is a scheme (ie http/ftp/feed etc) if there isn't any it will assume http and add it. Then it will get the document and looks for favicon. If there is any it will return the full path to the favicon. If favicon cannot be found it will return FALSE.

 

function getFavicon($url) {


   $check = parse_url($url);
   krumo($check);
   
   if(empty($check['scheme'])) {
    $url = 'http://' . ltrim($url, '/');       
    }
              
   $url = parse_url($url);    
   if (!empty($url['host'])) { // Get host path & check if smth is there
       $url  = $url['host'];
            $url = $url['scheme'] . ltrim($url, '/'); // put back original scheme
       }
    else {
        return false;
    }
   
    $href = false;
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT ,2); 
    curl_setopt($ch, CURLOPT_TIMEOUT, 2); //timeout in seconds   
    $content = curl_exec($ch);
    if (!empty($content)) {
      $dom = new DOMDocument();
      @$dom->loadHTML($content); //supress errors
      $items = $dom->getElementsByTagName('link');
      foreach ($items as $item) {
         $rel = $item->getAttribute('rel');
         if ($rel == 'icon' or $rel == 'shortcut icon') {
            $href = $item->getAttribute('href');
            break;
         }
      }
   }
   if ($href != false) {
    $href = parse_url($href);
    return $url . $href['path'];
    }
} 
Link to comment
https://forums.phpfreaks.com/topic/288188-get-favicon/#findComment-1478016
Share on other sites

I can't edit my previous post .. ? below is newer function, removed some mistakes

function getFavicon($url) {

   $check = parse_url($url);
   
   if(empty($check['scheme'])) {   // check if there http whatever, if not add it.
    $url = 'http://' . ltrim($url, '/');       
    }
    
   $check = parse_url($url);  
   if (!empty($check['host'])) { // Get host path (thats all we need , get rid of path)  check if smth is there
       $url = $check['host'];
       $url = $check['scheme'] . '://' . $url; // put back original scheme
       }
    else {
        return false;
    }
   
    $href = false;
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT ,2); 
    curl_setopt($ch, CURLOPT_TIMEOUT, 2); //timeout in seconds   
    $content = curl_exec($ch);
    if (!empty($content)) {
      $dom = new DOMDocument();
      @$dom->loadHTML($content); //supress errors
      $items = $dom->getElementsByTagName('link');
      foreach ($items as $item) {
         $rel = $item->getAttribute('rel');
         if ($rel == 'icon' or $rel == 'shortcut icon') {
            $href = $item->getAttribute('href');
            break;
         }
      }
   }
   if ($href != false) {
    $href = parse_url($href);
    return $url . $href['path'];
    }
return false;
} 

 

Link to comment
https://forums.phpfreaks.com/topic/288188-get-favicon/#findComment-1478017
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.