Curl and sended response

Discussion in 'PHP' started by livre, Nov 2, 2009.

  1. #1
    Hello guys..

    I spend a whole night searching and trying, please give some help...

    I want to check the existance of some media files (avi, mp4, flv .....)
    if the files exists or not exist anymore.

    I have the direct links of these media files, so here's the code that i used :
    
    $userAgent ='# IE 6 - Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)';
    $ch = curl_init();
    curl_setopt ($ch, CURLOPT_USERAGENT, $userAgent);
    curl_setopt ($ch, CURLOPT_URL, "LINK OF THE MEDIA FILE");
    curl_setopt ($ch, CURLOPT_HEADER, 0);
    
    curl_exec ($ch);
    $result=curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close ($ch);
    echo $result;
    
    Code (markup):
    when the file don't exist the $result returns '404' but i don't want the page of the browser appear :

    
    The page cannot be found
    The page you are looking for might have been removed, had its name changed, or is temporarily unavailable.
    
    Please try the following:
    
    ............................................
    
    Code (markup):
    And when the file exist the page become a huge ascii code... and the browser become not responding...

    Like I said i want just the ode send '404' for not found files and 'OK' for founded files.

    Did i miss something.
    Thanks for your help.
     
    livre, Nov 2, 2009 IP
  2. organicCyborg

    organicCyborg Peon

    Messages:
    330
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You'll need to fetch the response codes through the header of the site.

    Found this example on PHP.net

    
    <?php
    
    /*==================================
    Get url content and response headers (given a url, follows all redirections on it and returned content and response headers of final url)
    
    @return    array[0]    content
            array[1]    array of response headers
    ==================================*/
    function get_url( $url,  $javascript_loop = 0, $timeout = 5 )
    {
        $url = str_replace( "&amp;", "&", urldecode(trim($url)) );
    
        $cookie = tempnam ("/tmp", "CURLCOOKIE");
        $ch = curl_init();
        curl_setopt( $ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3) Gecko/20041001 Firefox/0.10.1" );
        curl_setopt( $ch, CURLOPT_URL, $url );
        curl_setopt( $ch, CURLOPT_COOKIEJAR, $cookie );
        curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
        curl_setopt( $ch, CURLOPT_ENCODING, "" );
        curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
        curl_setopt( $ch, CURLOPT_AUTOREFERER, true );
        curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );    # required for https urls
        curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, $timeout );
        curl_setopt( $ch, CURLOPT_TIMEOUT, $timeout );
        curl_setopt( $ch, CURLOPT_MAXREDIRS, 10 );
        $content = curl_exec( $ch );
        $response = curl_getinfo( $ch );
        curl_close ( $ch );
    
        if ($response['http_code'] == 301 || $response['http_code'] == 302)
        {
            ini_set("user_agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3) Gecko/20041001 Firefox/0.10.1");
    
            if ( $headers = get_headers($response['url']) )
            {
                foreach( $headers as $value )
                {
                    if ( substr( strtolower($value), 0, 9 ) == "location:" )
                        return get_url( trim( substr( $value, 9, strlen($value) ) ) );
                }
            }
        }
    
        if (    ( preg_match("/>[[:space:]]+window\.location\.replace\('(.*)'\)/i", $content, $value) || preg_match("/>[[:space:]]+window\.location\=\"(.*)\"/i", $content, $value) ) &&
                $javascript_loop < 5
        )
        {
            return get_url( $value[1], $javascript_loop+1 );
        }
        else
        {
            return array( $content, $response );
        }
    }
    
    ?>
    Code (markup):
    So, you can check $response['http_code'] to see if it's a 404.
     
    organicCyborg, Nov 3, 2009 IP