Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2883
  • Last Modified:

PHP cURL call with POST vars returning false but cURL Linux command

Hi,

I'm wondering why this bit of code is returning false. I had to remove the pass, signature and email for security reasons:

<?php
//set POST variables
$url = 'https://api-3t.paypal.com/nvp';

$METHOD = "BMCreateButton";
$VERSION = "109";
$USER = "my.gmail.com";
$PWD = "mypassword";
$SIGNATURE = "mysignature";
$BUTTONCODE = "CLEARTEXT";
$L_BUTTONVAR1 = "item_name=Dynamic"; //Dynamic MyUltraTrust.com Standard Irrevocable Trust Package
$L_BUTTONVAR2 = "item_number=dynamic_installment_plan"; //dynamic_installment_plan
$BUTTONTYPE = "PAYMENTPLAN";
$BUTTONSUBTYPE = "SERVICES";
$OPTION0NAME = "Special Televesion";

$L_OPTION0SELECT0 = "First";
$OPTION0TYPE = "FULL";
$L_OPTION0PRICE0 = "2703.00";

$L_OPTION0SELECT1 = "Second";
$OPTION1TYPE = "EMI";
$L_OPTION1AMOUNT0 = "901.00";
$L_OPTION1BILLINGFREQUENCY0 = "1";
$L_OPTION1BILLINGPERIOD0 = "Month";
$L_OPTION1TOTALBILLINGCYCLES0 = "3";

$fields = array(
    'METHOD' => urlencode($METHOD),
    'VERSION' => urlencode($VERSION),
    'USER' => urlencode($USER),
    'PWD' => urlencode($PWD),
    'SIGNATURE' => urlencode($SIGNATURE),
    'BUTTONCODE' => urlencode($BUTTONCODE),
    'L_BUTTONVAR1' => urlencode($L_BUTTONVAR1),
    'L_BUTTONVAR2' => urlencode($L_BUTTONVAR2),
    'BUTTONTYPE' => urlencode($BUTTONTYPE),
    'BUTTONSUBTYPE' => urlencode($BUTTONSUBTYPE),
    'OPTION0NAME' => urlencode($OPTION0NAME),

    'L_OPTION0SELECT0' => urlencode($L_OPTION0SELECT0),
    'OPTION0TYPE' => urlencode($OPTION0TYPE),
    'L_OPTION0PRICE0' => urlencode($L_OPTION0PRICE0),

    'L_OPTION0SELECT1' => urlencode($L_OPTION0SELECT1),
    'OPTION1TYPE' => urlencode($OPTION1TYPE),
    'L_OPTION1AMOUNT0' => urlencode($L_OPTION1AMOUNT0),
    'L_OPTION1BILLINGFREQUENCY0' => urlencode($L_OPTION1BILLINGFREQUENCY0),
    'L_OPTION1BILLINGPERIOD0' => urlencode($L_OPTION1BILLINGPERIOD0),
    'L_OPTION1TOTALBILLINGCYCLES0' => urlencode($L_OPTION1TOTALBILLINGCYCLES0)
);

//url-ify the data for the POST
$fields_string = '';
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');

//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);

//execute post
$result = curl_exec($ch);

//close connection
curl_close($ch);
?>

Open in new window


But this exact same POST vars and cURL call from the Linux command line works:

curl -d "METHOD=BMCreateButton&VERSION=109&USER=my.gmail.com&PWD=mypassword&SIGNATURE=mysignature&BUTTONCODE=CLEARTEXT&L_BUTTONVAR1=item_name%3DDynamic&L_BUTTONVAR2=item_number%3Ddynamic_installment_plan&BUTTONTYPE=PAYMENTPLAN&BUTTONSUBTYPE=SERVICES&OPTION0NAME=Special+Televesion&L_OPTION0SELECT0=First&OPTION0TYPE=FULL&L_OPTION0PRICE0=2703.00&L_OPTION0SELECT1=Second&OPTION1TYPE=EMI&L_OPTION1AMOUNT0=901.00&L_OPTION1BILLINGFREQUENCY0=1&L_OPTION1BILLINGPERIOD0=Month&L_OPTION1TOTALBILLINGCYCLES0=3" https://api-3t.paypal.com/nvp

Open in new window


What's wrong with the PHP code?
0
Victor Kimura
Asked:
Victor Kimura
  • 2
1 Solution
 
Ray PaseurCommented:
Here is my teaching example of how to do a cURL POST request.  See if you can adapt it to your needs.  There are a lot of moving parts to something like this and the options play together in "interesting" ways!
<?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=FALSE)
{
    // 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
 
Victor KimuraAuthor Commented:
Thanks @Ray! Yes, that works.
0
 
Ray PaseurCommented:
Great!  Thanks for the points and thanks for using EE, ~Ray
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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