/    Sign up×
Community /Pin to ProfileBookmark

curl_exec not catching timeout/no connection

Fatal error: Maximum execution time of 60 seconds exceeded in C:wampwwwpathtoscriptCRMPicco.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 error and curl_exec doesn’t throw and exception which would be caught by the try block?

[code=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, “www.crmpicco.co.uk/testurl.php”);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 30); // timing out after 191secs
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”);
}
[/code]

Cheers,
Picco

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@NogDogSep 16.2009 — The PHP cURL functions do not throw exceptions, so there will never be anything to catch. You'll just have to check the return value for a boolean false.

As to why the cURL timeout does not seem to be occurring after 30 seconds, I'm not sure. ?
Copy linkTweet thisAlerts:
@crmpiccoauthorSep 17.2009 — 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=php]

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]
×

Success!

Help @crmpicco spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.6,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...