Solved

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

Posted on 2014-01-20
3
2,399 Views
Last Modified: 2014-01-21
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
Comment
Question by:Victor Kimura
  • 2
3 Comments
 
LVL 110

Accepted Solution

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

Author Comment

by:Victor Kimura
ID: 39796308
Thanks @Ray! Yes, that works.
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 39796518
Great!  Thanks for the points and thanks for using EE, ~Ray
0

Featured Post

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to change the colour of columns using dynamic data for google charts 3 41
$_GET call between URL 3 37
while read ID; do 4 53
IMAP copying tool 14 43
Linux users are sometimes dumbfounded by the severe lack of documentation on a topic. Sometimes, the documentation is copious, but other times, you end up with some obscure "it varies depending on your distribution" over and over when searching for …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

685 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