cURL / Mailing Lists / curl-and-php / Single Mail

curl-and-php

curl_exec() dies intermittently

From: Richard Draucker <rd_at_traceli.com>
Date: Thu, 6 Sep 2007 08:57:59 -0500

My application repeatedly requests pages from a given site. Some sites work just fine. Others, however, intermittently hang while executing curl_exec().

The code...
function getSrcHTML($target){

        $ch = curl_init();

        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_VERBOSE, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_MAXREDIRS, 2);

        // IMITATE CLASSIC BROWSER BEHAVIOR
        curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
        curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiefile);
        curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiefile);

        // HANDLE SSL CONNECTION
        if(strpos($target,"https") !== false){
                curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        }

        // RETRIEVE TARGET PAGE
        curl_setopt($ch, CURLOPT_URL, $target);
        $html = curl_exec($ch);

        if(curl_error($ch) !== ''){
                flushMsg(curl_error($ch));
                curl_close($ch);
                return "<html><head></head><body><table><tr><td></td></tr></table></body></html>";
        }

        curl_close($ch);

        return $html;
}

Server Info: CentOS with PHP 5.2.0 on a VPS

Any suggestions as to how to prevent curl_exec() from hanging or how to make CURLOPT_TIMEOUT force a return after the specified time period?

Thanks,
Richard Draucker <rd_at_traceli.com>
_______________________________________________
http://cool.haxx.se/cgi-bin/mailman/listinfo/curl-and-php
Received on 2007-09-06