php获取网站ico

php获取网站ico

/*
 * 获取网站logo
 */
function get_url_ico($url){
    $url_arr=parse_url($url);
    if(!$url_arr['scheme']){
        $url.="http://";
    }
    $url_arr=parse_url($url);
    $url=$url_arr['scheme']."://".$url_arr['host'];
    if(url_exists($url)){
        $api_url="http://g.soz.im/{$url}/cdn.ico";
        $ico=$url."/favicon.ico";
        if(remote_file_exists($ico)){
            return $ico;
        }elseif(remote_file_exists($api_url)){
            return $api_url;
        }else{
            return false;
        }
    }else{
        return false;
    }
}

/*
 * 判断url是否存在
 */
function url_exists($url)   
{  
   $head = @get_headers($url);  
   return is_array($head) ?  true : false;  
}

/*
 * 判断远程文件是否存在
 */
function remote_file_exists($url) {
    $executeTime = ini_get('max_execution_time');
    ini_set('max_execution_time', 0);
    $headers = @get_headers($url);
    ini_set('max_execution_time', $executeTime);
    if ($headers) {
        $head = explode(' ', $headers[0]);
        if ( !empty($head[1]) && intval($head[1]) < 400) return true;
    }
    return false;
}

 

发表评论

邮箱地址不会被公开。 必填项已用*标注