当前位置:首页 »“秋了秋”个人博客 » 网络技术 » php获取友情链接网站的favicon图标接口文档

php获取友情链接网站的favicon图标接口文档

作者:秋了秋 发表时间:2014年11月30日

这是一个api接口文档,之前有一篇文章是介绍如何获取favicon图标的,具体可以看这篇文章《给网站的友情链接前面自动添加favicon图标》,由于考虑到独立性的优越性,现在把这个代码共享出来,让各自自己实现。这段代码只能抓取favicon.ico格式的图片,其它格式的抓取不到,所以还要手动添加.ico图片进缓存文件夹。

新建个php文档,命名为index.php,把以下代码复制进去:

<?php $url = @$_GET['url']; if($url){$url = preg_replace('/http\:\/\//i','',$url);$url = 'http://'.$url;$domain = parse_url($url);$url = $domain['host'];$dir = 'images';$fav = $dir."/".$url.".ico"; header('Content-type: image/png'); $file = @file_get_contents($fav); if($file){echo $file;exit;}$file = @file_get_contents("http://$url/favicon.ico"); if($file){$f2 = $file;echo $f2;}else{$w = @file_get_contents("http://$url/",0,null,0,2000); @preg_match('|href=\"(.*?)\.ico\"|i',$w,$a); if($a[1]){$a[1] .='.ico';$f = @file_get_contents($a[1]);if($f){echo $f;}else{$u = 'http://'.$url.'/'.$a[1];$f2 = @file_get_contents($u); if($f2){echo $f2;}else{$f2 = @file_get_contents('//img.netblog.site/url.jpg');echo $f2;}}}else{$f2 = @file_get_contents('//img.netblog.site/url.jpg');echo $f2;}} if($f2)@file_put_contents($fav,$f2);}else{ header("Content-Type:text/html;charset=utf-8"); echo '示例://netblog.cn/api/?url=netblog.cn'; ?><br /> 此接口可以直接放到img的src之后使用,即<pre>&ltimg src="//netblog.cn/api/?url=netblog.cn"&gt</pre><br /> 缓存目录为:<a href="//netblog.cn/api/images/" target="_blank">//netblog.cn/api/images/</a><br/> 已经缓存的文件ico图标文件:<br/> <?php $dir = "images/"; if (is_dir($dir)){if ($dh = opendir($dir)){while (($file = readdir($dh)) !== false){ echo "文件名: $file <br>";}closedir($dh);}}?> <?php } ?>

然后在网站根目录创建一个文件夹为api,把这个php文档放到api文件夹下,同时在api文件夹内再创建一个images文件夹,确保有可写权限。使用方法详见《给网站的友情链接前面自动添加favicon图标》。

核心代码来源于网络。

0
文章作者: “秋了秋”个人博客,本站鼓励原创。
转载请注明本文地址:http://netblog.cn/blog/183.html
目录: 网络技术标签: 友情链接图标获取 10258次阅读

请求播放音乐,请点击播放

登 录
点击获取验证码
还没账号?点击这里