Debugging curl_exec
<p>I have been having a very difficult time recently debugging a curl function within our paypal ipn module.</p>

<p>Here's the module we are using: <a href="http://www.oscommerce.com/community/contributions,2679" rel="nofollow">http://www.oscommerce.com/community/contributions,2679</a> But the relevant code is below.</p>

<p>I am experiencing this error for about 5% of our transactions. It causes orders not to be updated to a paid status.</p>

<p>I have determined that it's curl_exec which is failing and stopping further scripts being processed. There are no errors related to this in the error log on cpanel. </p>

<p>I have tried altering both the curl and php time out durations with no change. I have tried using try and catch but that didn't help either. </p>

<p>As the scripts stop being executed when it hits curl_exec i can't use curl_error to get any feedback about the problem.</p>

<p>I have looked through the parameters which are being passed to the IPN but they are fine.</p>

<p>I feel like I have tried all I can to determine the cause of this problem and got no where. </p>

<p>I experience the same problem if the script uses fsock as it's primary method of connecting. </p>

<p>If anyone has any suggestions on other methods of debugging this problem i'd appreciate it.</p>

<p>Thanks</p>

<p><code>
if (MODULE_PAYMENT_PAYPAL_IPN_GATEWAY_SERVER == 'Live') {
 $server = 'www.paypal.com';
} else {
 $server = 'www.sandbox.paypal.com';
}</p>

<p>$fsocket = false;
$curl = false;
$result = false;</p>

<p>if (function_exists('curl_exec')) {
 $curl = true;
} elseif ((PHP_VERSION >= 4.3) &amp;&amp; ($fp = @fsockopen('ssl://' . $server, 443, $errno, $errstr, 30)) ) {
 $fsocket = true;
} elseif ($fp = @fsockopen($server, 80, $errno, $errstr, 30)) {
 $fsocket = true;
}</p>

<p>if ($fsocket == true) {
 $header = 'POST /cgi-bin/webscr HTTP/1.0' . "\r\n" .
              'Host: ' . $server . "\r\n" .
              'Content-Type: application/x-www-form-urlencoded' . "\r\n" .
              'Content-Length: ' . strlen($parameters) . "\r\n" .
              'Connection: close' . "\r\n\r\n";</p>

<p>@fputs($fp, $header . $parameters);</p>

<p>$string = '';
 while (!@feof($fp)) {
  $res = @fgets($fp, 1024);
  $string .= $res;</p>

<p>if ( ($res == 'VERIFIED') || ($res == 'INVALID') ) {
   $result = $res;</p>

<p>break;
  }
 }</p>

<p>@fclose($fp);</p>

<p>} elseif ($curl == true) {
 $ch = @curl_init();</p>

<p>@curl_setopt($ch, CURLOPT_URL, 'https://' . $server . '/cgi-bin/webscr');
 @curl_setopt($ch, CURLOPT_POST, true);
 @curl_setopt($ch, CURLOPT_POSTFIELDS, $parameters);
 @curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 @curl_setopt($ch, CURLOPT_HEADER, false);
 @curl_setopt($ch, CURLOPT_TIMEOUT, 30);
 @curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);</p>

<p>$result = @curl_exec($ch);</p>

<p>curl_close($ch);
}</code></p>
