Solved

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

Posted on 2014-01-20
3
2,450 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
This article discusses four methods for overlaying images in a container on a web page
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.:
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

738 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