修复错误
增加刷新缓存参数:refresh=true
增加获取数据失败时自动查询第三方api数据
master
owen 2023-02-20 15:01:17 +08:00
parent 0dc1a2a91a
commit edee81fc2f
2 changed files with 58 additions and 45 deletions

View File

@ -251,10 +251,10 @@ class Favicon
* FIX #1
* 对取到的HTML内容进行删除换行符的处理,避免link信息折行导致的正则匹配失败
*/
$html = str_replace(array("\n", "\r"), '', $html);
$html = str_replace(array("\n", "\r"), '', $html['data']);
//匹配完整的LINK标签再从LINK标签中获取HREF的值
if (@preg_match('/((<link[^>]+rel=.(icon|shortcut icon|alternate icon|apple-touch-icon)[^>]+>))/i', $html['data'], $match_tag)) {
if (@preg_match('/((<link[^>]+rel=.(icon|shortcut icon|alternate icon|apple-touch-icon)[^>]+>))/i', $html, $match_tag)) {
if (isset($match_tag[1]) && $match_tag[1] && @preg_match('/href=(\'|\")(.*?)\1/i', $match_tag[1], $match_url)) {
@ -309,18 +309,16 @@ class Favicon
/**
* 从其他api最后获取图像 -----------------------------------------------------
*
* t3.gstatic.com 国内可用 t3.gstatic.cn
*/
//if ($this->data == NULL) {
// $thrurl='http://www.google.cn/s2/favicons?domain=';
// $icon = file_get_contents($thrurl.$this->full_host);
// //$this->_log_message("--图标 md5 值为".md5($icon));
// if($icon && md5($icon)!="3ca64f83fdcf25135d87e08af65e68c9"){ //判断是否为对方 api 返回的默认图标,可通过上行 log 查看
// $this->_log_message("--从 {$thrurl} 获取到图标");
// $this->data = $icon;
// }
//}
if ($this->data == NULL) {
$thrurl='http://t3.gstatic.cn/faviconV2?client=SOCIAL&type=FAVICON&fallback_opts=TYPE,SIZE,URL&size=128&url='.$this->full_host;
$icon = file_get_contents($thrurl);
if($icon){
$this->_log_message("--https://t3.gstatic.com/{$this->full_host}/favicon.ico");
$this->data = $icon;
}
}
if ($this->data == NULL) {
//各个方法都试过了,还是获取不到。。。
@ -469,7 +467,7 @@ class Favicon
* @param string $url
* @param bool $isimg 是否为图片
* @param int $timeout 超时值默认为10秒
* @return string 成功返回获取到的内容,同时设置 $this->content失败返回FALSE
* @return string|array 成功返回获取到的内容,同时设置 $this->content失败返回FALSE
*/
private function getFile($url, $isimg = false, $timeout = 2)
{
@ -483,6 +481,13 @@ class Favicon
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
/** @var mixed 只获取500kb的数据如果目标图片超过500kb则丢弃 */
$request_headers = array('Range: bytes=0-512000'); //500 KB
curl_setopt( $ch, CURLOPT_FORBID_REUSE, true );
$request_headers[] = 'Connection: close';
curl_setopt( $ch, CURLOPT_HTTPHEADER, $request_headers );
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
//执行重定向获取
$ret = $this->curlExecFollow($ch, 2);
@ -501,7 +506,7 @@ class Favicon
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$arr = array(
'status' => ($status >= 200 && $status <= 299) ? TRUE : FALSE,
'status' => ($status >= 200 && $status <= 299) ? 'OK' : 'FAIL',
'data' => $ret,
'real_url' => curl_getinfo($ch, CURLINFO_EFFECTIVE_URL)
);

68
get.php
View File

@ -4,7 +4,7 @@
* @author 一为
* @date 2019-11-27
* @link https://www.iowen.cn
* @version 1.1.0
* @version 1.2.0
*/
if( !isset($_GET['url'])){
@ -39,43 +39,50 @@ $url = $_GET['url'];
* 格式化 URL, 并尝试读取缓存
*/
$formatUrl = $favicon->formatUrl($url);
if($formatUrl){
if($expire == 0){
$favicon->getFavicon($formatUrl, false);
exit;
} else {
$defaultMD5 = md5(file_get_contents($defaultIco));
/**
* 2023-02-20
* 增加刷新缓存参数refresh=true https://域名?url=www.iowen.cn&refresh=true
*/
if( !isset($_GET['refresh']) || ( isset($_GET['refresh']) && $_GET['refresh']!='true' ) ){
$data = Cache::get($formatUrl,$defaultMD5,$expire);
if ($data !== NULL) {
foreach ($favicon->getHeader() as $header) {
@header($header);
}
echo $data;
exit;
}
}
/**
* 缓存中没有指定的内容时, 重新获取内容并缓存起来
*/
$content = $favicon->getFavicon($formatUrl, TRUE);
if( md5($content) == $defaultMD5 ){
$expire = 43200; //如果返回默认图标设置过期时间为12小时。Cache::get 方法中需同时修改
}
Cache::set($formatUrl, $content, $expire);
if($expire == 0){
$favicon->getFavicon($formatUrl, false);
exit;
}
else{
$defaultMD5 = md5(file_get_contents($defaultIco));
$data = Cache::get($formatUrl,$defaultMD5,$expire);
if ($data !== NULL) {
foreach ($favicon->getHeader() as $header) {
@header($header);
}
echo $data;
echo $content;
exit;
}
/**
* 缓存中没有指定的内容时, 重新获取内容并缓存起来
*/
$content = $favicon->getFavicon($formatUrl, TRUE);
if( md5($content) == $defaultMD5 ){
$expire = 43200; //如果返回默认图标设置过期时间为12小时。Cache::get 方法中需同时修改
}
Cache::set($formatUrl, $content, $expire);
foreach ($favicon->getHeader() as $header) {
@header($header);
}
echo $content;
exit;
}else{
return http_response_code(404);
}
/**
* 缓存类
*/
@ -138,6 +145,7 @@ class Cache
$imgdata = fopen($a, "w") or die("Unable to open file!"); //w 重写 a追加
fwrite($imgdata, $value);
fclose($imgdata);
clearstatcache();
}
}
}