Solved

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

Posted on 2014-01-20
3
2,515 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

Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

Question has a verified solution.

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

3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Google Drive is extremely cheap offsite storage, and it's even possible to get extra storage for free for two years.  You can use the free account 15GB, and if you have an Android device..when you install Google Drive for the first time it will give…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

728 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