Reputation: 26281
How can I perform a cURL request and perform a given action if there is no response from the remote machine?
The script is running in the background, and I am not concerned about making the user wait.
<?php
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,'http://example.com/bla.php');
curl_setopt($ch,CURLOPT_FOLLOWLOCATION,true);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_POSTFIELDS,array('x'=>'cba!','y'=>'abc'));
if($result = curl_exec($ch)) {
//do this
}
else {
//do this if no response after 5 seconds
}
curl_close($ch);
?>
Upvotes: 0
Views: 60
Reputation: 1823
Use CURLOPT_CONNECTTIMEOUT and CURLOPT_TIMEOUT
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT ,5);
curl_setopt($ch, CURLOPT_TIMEOUT, 5); //timeout in seconds
Upvotes: 1