Last active
February 23, 2018 15:36
-
-
Save onigetoc/1d6d179145241957c5529ecda27ef20f to your computer and use it in GitHub Desktop.
Get Favicon V2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
echo "<h2>Get Favicons URLs</h2>"; | |
//$url = "https://mrmondialisation.org"; | |
//$url = "http://stackoverflow.com/"; | |
//$url = "http://changera.blogspot.ca/"; | |
//$url = "http://veteranstoday.com"; | |
$url = "http://www.2012un-nouveau-paradigme.com/"; | |
//$url = "http://www.stopmensonges.com/"; | |
//$url = "http://reseauinternational.net"; | |
//$url = "http://revision3.com/diggnation/"; // should be http://revision3.com | |
$favicon = iconprovider($url); | |
echo $favicon; | |
echo "<br>"; | |
$rss_icon = "https://s14.postimg.org/5ig97nc69/feed_icon.png"; | |
echo '<img src="' . $favicon . '" onerror="this.src=\''.$rss_icon.'\';">'; | |
function iconprovider($url) { | |
$urlData = parse_url($url); | |
$url = $urlData[ "scheme" ] . "://" . $urlData[ "host" ]; | |
//$html = file_get_contents($url); | |
$html = getContent($url); | |
$link = parseFavicon($html); | |
$path = $link; | |
if ((substr($link, 0, 7) == 'http://') || (substr($link, 0, 8) == 'https://')) { | |
// the current link is an "absolute" URL - parse it to get just the path | |
$parsed = parse_url($link); | |
$path = $parsed['path']; | |
} | |
if(substr($url, -1) == '/') { | |
$url = substr($url, 0, -1); | |
} | |
$re = '#^(?:https?:)?//(?:[a-z0-9-]+\.)*((?:[a-z0-9-]+\.)[a-z]+)#i'; | |
if (preg_match($re, $path)){ | |
echo "Match full URL: ".$path ; | |
$url = ""; | |
}else{ | |
echo "do not Match full URL: ".$path ; | |
} | |
echo "<br>"; | |
$favicon = $url . $path; | |
return $favicon; | |
} | |
function getContent($url) { // Find RSS feed in a web page url - a litle bit slower | |
$ch = curl_init($url); | |
curl_setopt($ch, CURLOPT_HEADER, 0); | |
curl_setopt($ch, CURLOPT_VERBOSE, 0); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible;)"); | |
curl_setopt($ch, CURLOPT_TIMEOUT,1000); | |
curl_setopt($ch, CURLOPT_URL, urldecode($url)); | |
$content = curl_exec($ch); | |
if( ($content = curl_exec($ch) ) === false) | |
{ | |
$content = file_get_contents($url); | |
return $content ; | |
} | |
else | |
{ | |
return $content ; | |
} | |
} | |
function parseFavicon($html) { | |
// Get the 'href' attribute value in a <link rel="icon" ... /> | |
// Also works for IE style: <link rel="shortcut icon" href="http://www.example.com/myicon.ico" /> | |
// And for iOS style: <link rel="apple-touch-icon" href="somepath/image.ico"> | |
$matches = array(); | |
// Search for <link rel="icon" type="image/png" href="http://example.com/icon.png" /> | |
preg_match('/<link.*?rel=("|\').*icon("|\').*?href=("|\')(.*?)("|\')/i', $html, $matches); | |
if (count($matches) > 4) { | |
return trim($matches[4]); | |
} | |
// Order of attributes could be swapped around: <link type="image/png" href="http://example.com/icon.png" rel="icon" /> | |
preg_match('/<link.*?href=("|\')(.*?)("|\').*?rel=("|\').*icon("|\')*?/i', $html, $matches); | |
if (count($matches) > 2) { | |
return trim($matches[2]); | |
} | |
// No match | |
return null; | |
} | |
function fixpath($p) { | |
$p=str_replace('\\','/',trim($p)); | |
return (substr($p,-1)!='/') ? $p.='/' : $p; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Test online: https://goo.gl/5no5Wc