[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Private
  • Views: 63
  • Last Modified:

How to fix failed communications with Payment Gateways in PHP using Curl/SOAP

My server is showing communication errors with the PayPal SDK, Google API, and other remote servers in the php error log.

Is a 1-2% failure rate for communicating with other servers acceptable?

Where do I start looking to optimize?  I am running a LAMP system.

Do I optimize something in php.ini for CURL and SOAP?
0
phpzilla
Asked:
phpzilla
  • 2
  • 2
  • 2
1 Solution
 
Andrew AngellCommented:
What is the timeout setting you have set?  You might want to try adding a little more time on that and see if that helps.
0
 
phpzillaAuthor Commented:
Timeout settings where?  php.ini, the gateway api, ...

How do I diagnose failed communications on linux, is there a log file?
0
 
Andrew AngellCommented:
Look into this:  http://www.php.net/manual/en/function.set-time-limit.php

And check this out:  http://stackoverflow.com/questions/2582057/setting-curls-timeout-in-php

The first one is about setting it up in php.ini or setting it in code.  The second one is regarding the CURL timeout specifically.
0
Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

 
Ray PaseurCommented:
Is a 1-2% failure rate for communicating with other servers acceptable?
"Acceptable" is a pretty broad term.  Some questions to help run this to ground...

Are you on a shared server?

What PHP functions are causing the "communications errors?"

Are you using cURL error reporting?

Have you tried fsockopen() to speak with the APIs?
0
 
phpzillaAuthor Commented:
using curl

Should I always set CURLOPT_CONNECTTIMEOUT and CURLOPT_TIMEOUT?

Are system defaults set somewhere for CURLOPT_CONNECTTIMEOUT and CURLOPT_TIMEOUT?

Does /etc/php5/apache2/phi.ini->max_execution_time affect CURLOPT_CONNECTTIMEOUT and CURLOPT_TIMEOUT or does max_execution_time get suspeneded with a call is made to an external system via curl?

is there a difference with curl in apache (browser) vs cli (command line cron jobs?)
0
 
Ray PaseurCommented:
That seems like several questions.  I'll try to help with the cURL part.  Here is my teaching example showing how to make a cURL POST-method request.  I do not know about CURLOPT_CONNECTTIMEOUT.  I use CURLOPT_TIMEOUT to prevent a "hang" condition.  If you don't do that your cURL connection time is added to your script execution time for as long as it takes the remote service to respond.  I don't know what the system default might be; I always set the value I want explicitly.

<?php // RAY_curl_post_example.php
error_reporting(E_ALL);


// DEMONSTRATE HOW TO USE CURL POST TO START AN ASYNCHRONOUS PROCESS


function curl_post($url, $post_array=array(), $timeout=2, $error_report=TRUE)
{
    // PREPARE THE POST STRING
    $post_string = NULL;
    foreach ($post_array as $key => $val)
    {
        $post_string .= $key . '=' . urlencode($val) . '&';
    }
    $post_string = rtrim($post_string, '&');

    // PREPARE THE CURL CALL
    $curl = curl_init();
    curl_setopt( $curl, CURLOPT_URL,            $url         );
    curl_setopt( $curl, CURLOPT_HEADER,         FALSE        );
    curl_setopt( $curl, CURLOPT_POST,           TRUE         );
    curl_setopt( $curl, CURLOPT_POSTFIELDS,     $post_string );
    curl_setopt( $curl, CURLOPT_ENCODING,       'gzip,deflate'  );
    curl_setopt( $curl, CURLOPT_TIMEOUT,        $timeout     );
    curl_setopt( $curl, CURLOPT_RETURNTRANSFER, TRUE         );
    curl_setopt( $curl, CURLOPT_FOLLOWLOCATION, TRUE  );

    // EXECUTE THE CURL CALL
    $htm = curl_exec($curl);
    $err = curl_errno($curl);
    $inf = curl_getinfo($curl);

    // ON FAILURE
    if (!$htm)
    {
        // PROCESS ERRORS HERE
        if ($error_report)
        {
            echo "CURL FAIL: $url TIMEOUT=$timeout, CURL_ERRNO=$err";
            echo "<pre>\n";
            var_dump($inf);
            echo "</pre>\n";
        }
        curl_close($curl);
        return FALSE;
    }

    // ON SUCCESS
    curl_close($curl);
    return $htm;
}


// USAGE EXAMPLE CREATES ASSOCIATIVE ARRAY OF KEY=>VALUE PAIRS
$args["name"]  = 'Ray';
$args["email"] = 'Ray.Paseur@Gmail.com';

// ACTIVATE THIS TO SEE THE ARRAY OF ARGS
// var_dump($args);

// SET THE URL
$url = "http://LAPRBass.com/RAY_bounce_post.php";

// CALL CURL TO POST THE DATA
$htm = curl_post($url, $args, 3, TRUE);

// SHOW WHAT CAME BACK, IF ANYTHING
if ($htm)
{
    echo "<pre>";
    echo htmlentities($htm);
}
else
{
    echo "NO RESPONSE YET FROM $url -- MAYBE BECAUSE IT IS RUNNING ASYNCHRONOUSLY";
}

Open in new window

0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now