0

I am trying to check existing file/urls. There is many solutions in online but they can't give me actual result. i think it happen because redirection. so i use a code from https://stackoverflow.com/a/12628971/1312043

its works fine but sometimes its not work perfectly. my code:

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;
}

For example if i want to check this url : isValidUrl("http://www.shawonbd.com.be/check_me.php") Its responds like ok but its wrong :( Is there any way to get perfect results? Thanks

Community
  • 1
  • 1
Shawon
  • 930
  • 2
  • 10
  • 19
  • You could check is 200 code responselike you do and... it's hard to come up with more :) you can make it good for one/few particular pages... but not whole internet ;) – matiit Oct 25 '13 at 13:14

1 Answers1

0

You can use get_headers(http://php.net/manual/en/function.get-headers.php) function.

Ramesh
  • 4,223
  • 2
  • 16
  • 24