wannabe21 Posted May 2, 2014 Share Posted May 2, 2014 Hi, What is the fastest way to get a favicon from any given url? Pls don't suggest to use http://www.google.com/s2/favicons?domain_url= Quote Link to comment Share on other sites More sharing options...
fastsol Posted May 2, 2014 Share Posted May 2, 2014 Well that link seems to be a pretty easy way, what are you trying to do exactly that you don't want to use that method? Quote Link to comment Share on other sites More sharing options...
wannabe21 Posted May 2, 2014 Author Share Posted May 2, 2014 I don't want to use it because it creates a dependancy. I 'just' want to fetch the url to the favicon from any given url. Quote Link to comment Share on other sites More sharing options...
fastsol Posted May 2, 2014 Share Posted May 2, 2014 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? Quote Link to comment Share on other sites More sharing options...
wannabe21 Posted May 3, 2014 Author Share Posted May 3, 2014 (edited) 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 May 3, 2014 by wannabe21 Quote Link to comment Share on other sites More sharing options...
wannabe21 Posted May 3, 2014 Author Share Posted May 3, 2014 (edited) 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 May 3, 2014 by wannabe21 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.