curl_exec not catching no connection/timeout - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

curl_exec not catching no connection/timeout

crmpiccocrmpicco Posts: 12Member
Fatal error: Maximum execution time of 60 seconds exceeded in C:wampwwwpath oscriptCRMPicco.inc.php on line 102

I am receiving this error with the following code, the domain that I am using has been removed from my hosts file for testing purposes so that I can test where there is no connection to the server for example. Does anyone know why I would be getting this fatal error? I realise that curl_exec doesn't throw exceptions, but I cannot seem to find a way to stop or handle the fatal error?

From the PHP documentation it reads that the CURLOPT_CONNECTTIMEOUT option should come into play when it cannot connect to the domain.

If I have not properly explained it before I removed the entry from my hosts file so that my machine could not lookup the IP, for example:
82.34.112.23 crmpicco-watp.co.uk

These are example values and are just for illustration.

[php]
// create the headers
$headers = array(
"POST HTTP/1.0",
"Content-type: text/xml;charset="utf-8"",
"Accept: text/xml",
"Cache-Control: no-cache",
"Pragma: no-cache",
"Content-length: ".strlen($xml)
);

// create a new cURL session
$ch = curl_init();

// set a range of options for this cURL session
curl_setopt($ch, CURLOPT_URL, "http://crmpicco-watp.co.uk/testurl.php");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 25);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);

// send the request and store the result in an array
try {
$data = curl_exec($ch); // <-- errors here
}
catch (Exception $e) {
debug($e->getMessage());
throw new Exception("Error with cURL request");
}
[/php]

Cheers,
Picco

Comments

  • crmpiccocrmpicco Posts: 12Member
    I managed to find a solution to this without having to use exceptions and the like. Please see the code below, I hope it can be of help to someone researching this code at some point in the future

    [code]
    protected function dispatch() {

    // .... code ....

    $dispatcher_conn_issue = false;

    // send the request off to Sabrix
    $data = curl_exec($ch);

    // check if we cannot find the host
    if (empty($data)) {
    // set a flag to donate a connection issue
    $dispatcher_conn_issue = true;

    $picco_info["status"] = "Failure";
    $picco_info["statusinfo"] = "Failed Request";

    }

    if (!$dispatcher_conn_issue) {

    // ... rest of my code ...

    }

    $this->piccoval = $picco_info; // member variable set here

    }
    [/code]
Sign In or Register to comment.