php 判断http 地址是否可访问

1.

function url_exists($url)    
{   
    $head = @get_headers($url);   
    return is_array($head) ?  true : false;   
}

2.

function url_exists($url)   
{   
    $ch = curl_init();   
    curl_setopt($ch, CURLOPT_URL,$url);   
    curl_setopt($ch, CURLOPT_NOBODY, 1); // 不下载   
    curl_setopt($ch, CURLOPT_FAILONERROR, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT,3); //超时时长,单位秒     
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);   
    return (curl_exec($ch)!==false) ? true : false;   
}

3.

function url_exists($url)    
{   
    return file_get_contents($url,0,null,0,1) ? true : false;   
}

4.

利用 http 状态码,其实都差不多啦

http://my.oschina.net/u/1163434/blog/171427

你可能感兴趣的:(判断地址是否可以访问)