Get Favicon From a Web Site Link Access External Files Using PHP

Get Favicon From a Web Site Link Access External Files Using PHP

Listening Video

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; ?>

0 Response to "Get Favicon From a Web Site Link Access External Files Using PHP"

Post a Comment