Zephni
Zephni

Reputation: 873

PayPal curl call no response using PHP

I have seen some of the suggestions for this question already but none of them have helped so far. I have this simple little class for making cURL calls, which definitely works coz I've tried it with other host calls. Here is the class:

class CurlHelper
{
    public static function Post($URL, $Fields = array(), $DieExplainError = false)
    {
        return self::Call(array(
            CURLOPT_URL             => $URL,
            CURLOPT_RETURNTRANSFER  => true,
            CURLOPT_POST            => count($Fields),
            CURLOPT_POSTFIELDS      => http_build_query($Fields)
        ), $DieExplainError);
    }

    public static function Call($Array, $DieExplainError = false)
    {
        $ch = curl_init();

        curl_setopt_array($ch, $Array);

        $Result = curl_exec($ch);

        if($DieExplainError && curl_error($ch))
            die(curl_error($ch));

        curl_close($ch);

        return $Result;
    }
}

This simply means I can specify a host and an array of values to post and the Post method returns whatever it's response is.

Then for my PayPal call I am doing the following:

$Response = CurlHelper::Post("https://api-3t.sandbox.paypal.com/nvp", array(
    CURLOPT_HEADER                      => false,
    CURLOPT_SSL_VERIFYPEER              => false,
    CURLOPT_SSL_VERIFYHOST              => false,
    "USER"                              => "user",
    "PWD"                               => "pass",
    "SIGNATURE"                         => "sig",
    "METHOD"                            => "SetExpressCheckout",
    "VERSION"                           => 86,
    "PAYMENTREQUEST_0_PAYMENTACTION"    => "SALE",
    "PAYMENTREQUEST_0_AMT"              => 19,
    "PAYMENTREQUEST_0_CURRENCYCODE"     => "GBP",
    "cancelUrl"                         => "myurl",
    "returnUrl"                         => "myurl"
), true);

die("test: ".print_r($Response));

As far as I can see this should at least return something? Even if it was an error, but I get absolutely nothing.

Have I dun goofed?

After Ghulam Ali's comment, he has pointed out that I was passing CURLOPT parameters into the post array rather than the actual curl options. So I have made these changes:

The Post method of CurlHelper:

public static function Post($URL, $Fields = array(), $DieExplainError = false)
    {
        return self::Call(array(
            CURLOPT_URL             => $URL,
            CURLOPT_RETURNTRANSFER  => true,
            CURLOPT_HEADER          => false,
            CURLOPT_SSL_VERIFYPEER  => false,
            CURLOPT_SSL_VERIFYHOST  => false,
            CURLOPT_POST            => count($Fields),
            CURLOPT_POSTFIELDS      => http_build_query($Fields)
        ), $DieExplainError);
    }

The PayPal call:

$Response = CurlHelper::Post("https://api-3t.sandbox.paypal.com/nvp", array(
    "USER"                              => "",
    "PWD"                               => "",
    "SIGNATURE"                         => "",
    "METHOD"                            => "SetExpressCheckout",
    "PAYMENTREQUEST_0_PAYMENTACTION"    => "SALE",
    "PAYMENTREQUEST_0_AMT"              => 19,
    "PAYMENTREQUEST_0_CURRENCYCODE"     => "GBP",
    "cancelUrl"                         => "",
    "returnUrl"                         => ""
), true);

But unfortunately I am still getting an SSL error. What has been said makes perfect sense but somehow still not working :(

Upvotes: 1

Views: 534

Answers (1)

Ghulam Ali
Ghulam Ali

Reputation: 1935

You are incorrectly sending the parameter to your Class function.

CURLOPT_HEADER                      => false,
CURLOPT_SSL_VERIFYPEER              => false,
CURLOPT_SSL_VERIFYHOST              => false,

These are the curl options not the Post variables. You are sending these options as the Post variables. One way is to update your Curl function.

return self::Call(array(
    CURLOPT_URL             => $URL,
    CURLOPT_HEADER          => false,
    CURLOPT_SSL_VERIFYPEER  => false,
    CURLOPT_SSL_VERIFYHOST  => false,
    CURLOPT_RETURNTRANSFER  => true,
    CURLOPT_POST            => count($Fields),
    CURLOPT_POSTFIELDS      => http_build_query($Fields)
), $DieExplainError);

And call your class function like this:

$Response = CurlHelper::Post("https://api-3t.sandbox.paypal.com/nvp", array(
    "USER"                              => "user",
    "PWD"                               => "pass",
    "SIGNATURE"                         => "sig",
    "METHOD"                            => "SetExpressCheckout",
    "VERSION"                           => 86,
    "PAYMENTREQUEST_0_PAYMENTACTION"    => "SALE",
    "PAYMENTREQUEST_0_AMT"              => 19,
    "PAYMENTREQUEST_0_CURRENCYCODE"     => "GBP",
    "cancelUrl"                         => "myurl",
    "returnUrl"                         => "myurl"
));

Upvotes: 1

Related Questions