SolvedPrivate

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

Posted on 2014-02-18
6
44 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
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
Comment Utility
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 108

Accepted Solution

by:
Ray Paseur earned 500 total points
Comment Utility
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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Setting up Secure Ubuntu server on VMware 1.      Insert the Ubuntu Server distribution CD or attach the ISO of the CD which is in the “Datastore”. Note that it is important to install the x64 edition on servers, not the X86 editions. 2.      Power on th…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

743 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now