Friday, June 21, 2013

Get Favicon From other Website Using PHP

6/21/2013


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.

# USAGE EXAMPLE 

<?php
/* 
Online PHP Examples with Source Code
website: http://4evertutorials.blogspot.in/
*/



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://4evertutorials.blogspot.com/"; # replace with your url
$favicon = getfavicon($website);
echo $favicon;

?>

helpful? Share this

The Editorial Team of 4everTutorials consists of a group of PHP Professionals.

0 comments:

 

© 2014 4everTutorials. All rights resevered.

Back To Top