2013-10-25 6 views
0

기존 파일/URL을 확인하려고합니다. 온라인에는 많은 솔루션이 있지만 실제 결과를 제공 할 수는 없습니다. 리디렉션 때문에 발생한다고 생각합니다. 그래서 나는 코드를 사용한다. https://stackoverflow.com/a/12628971/1312043원격 URL을 확인하고 리디렉션을 방지하는 방법

잘 작동하지만 때때로 완벽하게 작동하지 않는다. 내 코드 :

예를 들어
function isValidUrl($url){ 
    // first do some quick sanity checks: 
    if(!$url || !is_string($url)){ 
     return false; 
    } 
    // quick check url is roughly a valid http request: (http://blah/...) 
    if(! preg_match('/^http(s)?:\/\/[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(\/.*)?$/i', $url)){ 
     return false; 
    } 
    // the next bit could be slow: 
    if(getHttpResponseCode_using_curl($url) != 200){ 
     return false; 
    } 
    // all good! 
    return true; 
} 

function getHttpResponseCode_using_curl($url, $followredirects = false){ 
    // returns int responsecode, or false (if url does not exist or connection timeout occurs) 
    // NOTE: could potentially take up to 0-30 seconds , blocking further code execution (more or less depending on connection, target site, and local timeout settings)) 
    // if $followredirects == false: return the FIRST known httpcode (ignore redirects) 
    // if $followredirects == true : return the LAST known httpcode (when redirected) 
    if(! $url || ! is_string($url)){ 
     return false; 
    } 
    $ch = @curl_init($url); 
    if($ch === false){ 
     return false; 
    } 
    @curl_setopt($ch, CURLOPT_HEADER   ,true); // we want headers 
    @curl_setopt($ch, CURLOPT_NOBODY   ,true); // dont need body 
    @curl_setopt($ch, CURLOPT_RETURNTRANSFER ,true); // catch output (do NOT print!) 
    if($followredirects){ 
     @curl_setopt($ch, CURLOPT_FOLLOWLOCATION ,true); 
     @curl_setopt($ch, CURLOPT_MAXREDIRS  ,10); // fairly random number, but could prevent unwanted endless redirects with followlocation=true 
    }else{ 
     @curl_setopt($ch, CURLOPT_FOLLOWLOCATION ,false); 
    } 
//  @curl_setopt($ch, CURLOPT_CONNECTTIMEOUT ,5); // fairly random number (seconds)... but could prevent waiting forever to get a result 
//  @curl_setopt($ch, CURLOPT_TIMEOUT  ,6); // fairly random number (seconds)... but could prevent waiting forever to get a result 
//  @curl_setopt($ch, CURLOPT_USERAGENT  ,"Mozilla/5.0 (Windows NT 6.0) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.89 Safari/537.1"); // pretend we're a regular browser 
    @curl_exec($ch); 
    if(@curl_errno($ch)){ // should be 0 
     @curl_close($ch); 
     return false; 
    } 
    $code = @curl_getinfo($ch, CURLINFO_HTTP_CODE); // note: php.net documentation shows this returns a string, but really it returns an int 
    @curl_close($ch); 
    return $code; 
} 

내가 URL을 확인하려면 : isValidUrl ("http://www.shawonbd.com.be/check_me.php")를 그것 괜찮 같은 응답하지만 잘못된 :( 완벽한 결과를 얻을 수있는 방법이 있나요 감사

?
+0

을 사용할 수 있습니다 : 하나/몇 가지 특정 페이지가 잘 할 수 있습니다 .. . 그러나 전체의 인터넷이 아니라) – matiit

답변

0

당신은 더 마련하기 어렵다 ... 당신이하고 responselike 200 코드는 당신이 확인할 수 있습니다 get_headers (http://php.net/manual/en/function.get-headers.php) 함수.

+0

그 pr 다 이벤트 리디렉션? – Shawon

+0

여기에는 리디렉션이 없습니다. var_dump (get_headers ($ url, 1)); // 모든 헤더 필드가있는 배열의 응답 – Ramesh

관련 문제