Get Favicon From Web Site Link Access External Files

Published :
Author :
Adam Khoury
Learn to get and render favicon.ico files dynamically from other servers in your PHP scripts. You can look for files and also process the DOM structure of external pages on the web. I recommend saving the favicon image file to your server converting it to a JPG or PNG file if it will be a frequently viewed image. Example 1 <?php $external_file = "http://www.developphp.com/favicon.ico"; $headers = get_headers($external_file); if(preg_match("|200|", $headers[0])) { echo "The file exists"; } else { echo "The file does not exist"; } ?> Example 2 <?php function getfavicon($url){ $favicon = ''; $html = file_get_contents($url); $dom = new DOMDocument(); $dom->loadHTML($html); $links = $dom->getElementsByTagName('link'); for ($i = 0; $i < $links->length; $i++){ $link = $links->item($i); if($link->getAttribute('rel') == 'icon'){ $favicon = $link->getAttribute('href'); } } return $favicon; } $website = "http://www.adamkhoury.com"; $favicon = getfavicon($website); echo $favicon; ?>