简体   繁体   中英

Converting command line cURL request to PHP cURL request (PayPal API)

Here's PayPal's example of the command line cURL request to their Permissions API:

curl https://svcs.sandbox.paypal.com/Permissions/GetAccessToken \
-s  \
--insecure \
-H "X-PAYPAL-SECURITY-USERID: api_username" \
-H "X-PAYPAL-SECURITY-PASSWORD: api_password" \
-H "X-PAYPAL-SECURITY-SIGNATURE: api_signature" \
-H "X-PAYPAL-REQUEST-DATA-FORMAT: NV" \
-H "X-PAYPAL-RESPONSE-DATA-FORMAT: NV" \
-H "X-PAYPAL-APPLICATION-ID: app_id" \
-d requestEnvelope.errorLanguage=en_US \
-d token=token \
-d verifier=code

I attempted entering my credentials, token and verifier in the above command line cURL into my command line and received a successful response.

However when I tried with my PHP application, I am not even getting an failure response. Here's my php cURL request:

class PayPalSession
{
    private $devUsername;
    private $devPassword;
    private $devSignature;
    private $appId;

    public function __construct($devUsername, $devPassword, $devSignature, $applicationId, $format)
    {
        $this->devUsername = $devUsername;
        $this->devPassword = $devPassword;
        $this->devSignature = $devSignature;
        $this->applicationId = $applicationId;
        $this->format = $format;
    }


    /** sendHttpRequest
        Sends a HTTP request to the server for this session
        Input:  $data
        Output: The HTTP Response as a String
    */
    public function sendHttpRequest($data, $endpoint)
    {
        //build eBay headers using variables passed via constructor
        $headers = $this->buildPaypalHeaders();

        //initialise a CURL session
        $connection = curl_init();
        //set the server we are using (could be Sandbox or Production server)
        curl_setopt($connection, CURLOPT_URL, $endpoint);

        //stop CURL from verifying the peer's certificate
        curl_setopt($connection, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($connection, CURLOPT_SSL_VERIFYHOST, 0);

        //set the headers using the array of headers
        curl_setopt($connection, CURLOPT_HTTPHEADER, $headers);

        //set method as POST
        curl_setopt($connection, CURLOPT_POST, 1);

        //set the Json body of the request
        curl_setopt($connection, CURLOPT_POSTFIELDS, $data);

        //set it to return the transfer as a string from curl_exec
        curl_setopt($connection, CURLOPT_RETURNTRANSFER, 1);

        //Send the Request
        $response = curl_exec($connection);

        //print $response;

        //close the connection
        curl_close($connection);

        //return the response
        return $response;
    }



    /** buildPayPalHeaders
        Generates an array of string to be used as the headers for the HTTP request to PayPal
        Output: String Array of Headers applicable for this call
    */
    private function buildPaypalHeaders()
    {
        $headers = array (
            // API credentials for the API Caller account
            'X-PAYPAL-SECURITY-USERID: ' . $this->devUsername,
            'X-PAYPAL-SECURITY-PASSWORD: ' . $this->devPassword,
            'X-PAYPAL-SECURITY-SIGNATURE: ' . $this->devSignature,

            // Application ID
            'X-PAYPAL-APPLICATION-ID: ' . $this->applicationId,

            // Input and output formats
            'X-PAYPAL-REQUEST-DATA-FORMAT : ' . $this->format,
            'X-PAYPAL-RESPONSE-DATA-FORMAT : ' . $this->format,
        );

        return $headers;
    }
}

Here are the variables that are passed in the code above:

$format = 'NV';
$endpoint = 'https://svcs.sandbox.paypal.com/Permissions/GetAccessToken'
$data = "requestEnvelope%5BerrorLanguage%5D=en_US&token=$requestToken&verifier=$verificationCode"

..and of course $requestToken and $verifierCode was set to what was provided to me by PayPal.

I am suspecting my problem is in my $data that I'm passing to "CURLOPT_POSTFIELDS" . The format is supposed to be "NV" (name=value or key=value) but if you look at PayPal example "requestEnvelope.errorLanguage=en_US" this does not look like a simple key=value pair so I am not sure how to format this for the request.

I tried this:

$requestAsArray = array('requestEnvelope' => array("errorLanguage" => "en_US"), "token" => "$requestToken", "verifier" => "$verificationCode");

$data = http_build_query($requestAsArray);

But still did not receive a response from PayPal API. I also tried sending the request as JSON and changing $format to "JSON", but the API requires name=value/key=value requests.

Here's PayPal's documentation for this call to their API: https://developer.paypal.com/docs/classic/permissions-service/integration-guide/PermissionsUsing/

Any help would be much appreciated!

When you use the header in NVP format

'X-PAYPAL-REQUEST-DATA-FORMAT : NV'

the request array should be

    $requestAsArray = array(
       'requestEnvelope.errorLanguage' =>"en_US"
       , "token" => "$requestToken"
       , "verifier" => "$verificationCode"
    );

And then use

$data = http_build_query($requestAsArray);

Whe you use the header in json format

'X-PAYPAL-REQUEST-DATA-FORMAT : JSON'

The request array should be

$requestAsArray = array(
   'requestEnvelope' => array("errorLanguage" => "en_US")
   , "token" => "$requestToken"
   , "verifier" => "$verificationCode");

and then use

$data = json_encode($requestAsArray);

I use this methods in Paypal Adaptive Payment calls and it always works. It should work in this case too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM