Jump to content

PHP Curl Timeout Problem


seashore

Recommended Posts

Hi, Am a newbie to this forum & Php.

 

I use curl to post form data to multiple websites in sequence. Sometimes I get a curl timeout fatal error & the script stops running.  If one website timesout, I would like the script to keep running & move to the next website in the sequence.

 

// Use curl to post the data to the vendor
$ch = curl_init();

//Some servers (like Lighttpd) will not process the curl request without this header and will return error code 417 instead.
//Apache does not need it, but it is safe to use it there as well.
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Expect:"));

//Response will be read in chunks of 64000 bytes
curl_setopt($ch, CURLOPT_BUFFERSIZE, 64000);

//Tell curl to write the response to a variable
curl_setopt($ch, CURLOPT_TIMEOUT, 50);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

// Tell curl to ignore the SSL certificate
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, FALSE);

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,$this->vendor_url);
curl_setopt($ch,CURLOPT_POST,$this->fields_count);
curl_setopt($ch,CURLOPT_POSTFIELDS,$this->post_str);

//execute post
$response = curl_exec($ch);
if(empty($reponse))
{
return false;
}
else
{
$error = curl_error($ch);
$result = array( 'header' => '',
				 'body' => '',
				 'curl_error' => '',
				 'http_code' => '',
				 'last_url' => '');

if ( $error != "" )
		{
			$result['curl_error'] = $error;
			//return $result;
		}

$header_size = curl_getinfo($ch,CURLINFO_HEADER_SIZE);
$result['header'] = substr($response, 0, $header_size);
$result['body'] = substr( $response, $header_size );
$result['http_code'] = curl_getinfo($ch,CURLINFO_HTTP_CODE);
$result['last_url'] = curl_getinfo($ch,CURLINFO_EFFECTIVE_URL);


//get the default response headers
$headers = curl_getinfo($ch);

//close connection
curl_close($ch);

return $result;
}			

 

I get the fatal error on the line $error = curl_error($ch);

 

Would appreciate suggestions on how best to fix this.

 

Thanks,

Link to comment
https://forums.phpfreaks.com/topic/110385-php-curl-timeout-problem/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.