SolvedPrivate

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

Posted on 2014-02-18
6
50 Views
Last Modified: 2016-03-24
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
Comment
Question by:phpzilla
  • 2
  • 2
  • 2
6 Comments
 
LVL 11

Expert Comment

by:Andrew Angell
ID: 39869221
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
 

Author Comment

by:phpzilla
ID: 39869236
Timeout settings where?  php.ini, the gateway api, ...

How do I diagnose failed communications on linux, is there a log file?
0
 
LVL 11

Expert Comment

by:Andrew Angell
ID: 39869251
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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
LVL 109

Expert Comment

by:Ray Paseur
ID: 39870242
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
 

Author Comment

by:phpzilla
ID: 39870710
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
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 39871107
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to create an extensible mechanism for linked drop downs.
Fine Tune your automatic Updates for Ubuntu / Debian
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question