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
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'];
    }
} 
Edited by wannabe21
Link to comment
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;
} 

 

Edited by wannabe21
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.