Solved

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

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

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 108

Expert Comment

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

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

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.
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

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

10 Experts available now in Live!

Get 1:1 Help Now