0

我正在尝试使用 curl 使用 Paypal Adaptive API。

文档告诉我可以以 JSON 格式发送参数,但我总是Invalid Request (580001)出错。

这是我正在做的请求:
标题

[X-PAYPAL-SECURITY-USERID] => XXXXXX
[X-PAYPAL-SECURITY-PASSWORD] => XXXXXX
[X-PAYPAL-SECURITY-SIGNATURE] => XXXXXX
[X-PAYPAL-REQUEST-DATA-FORMAT] => JSON
[X-PAYPAL-RESPONSE-DATA-FORMAT] => JSON
[X-PAYPAL-APPLICATION-ID] => APP-80W284485P519543T   //APP-ID for Sandbox

这是我建立的 json 数据:

{"endingDate":"2012-06-11T12:20:02+00:00",
 "startingDate":"2012-06-20T12:20:02+00:00",
 "maxTotalAmountOfAllPayments":"1000.00",
 "currencyCode":"EUR",
 "cancelUrl":"http:\/\/localhost\/xx\/pledge?id=221&step=fail",
 "returnUrl":"http:\/\/localhost\/xx\/pledge?id=221&step=done",
 "pinType":"NOT_REQUIRED",
 "requestEnvelope":{"detailLevel":"ReturnAll","errorLanguage":"en_US"},
 "clientDetails":[]
}

我想我没有正确发送 JSON 数据。我正在设置这样的 JSON 数据(使用curl):

curl_setopt( $handle, CURLOPT_POST, true );
curl_setopt( $handle, CURLOPT_POSTFIELDS, $json_data );

这是发送json数据的正确方法吗?

4

1 回答 1

3

好的,我找到了解决方案:

我错过了Content-Type在标题和Content-Length.
所以最后的结果是这样的:

$json_data = json_encode( $json_fields );

$this->headers = array(
// Authentication
'X-PAYPAL-SECURITY-USERID'  => $this->_username,
'X-PAYPAL-SECURITY-PASSWORD'    => $this->_password,
'X-PAYPAL-SECURITY-SIGNATURE'   => $this->_signature,

// Data format
'X-PAYPAL-REQUEST-DATA-FORMAT'  => 'JSON',
'X-PAYPAL-RESPONSE-DATA-FORMAT' => 'JSON',

// Application and Device identification
'X-PAYPAL-APPLICATION-ID'   => $this->_app_id,
'X-PAYPAL-DEVICE-IPADDRESS' => $this->_client_ip,
'Content-Type'          => 'application/json',
    'Content-Length'                => strlen( $json_data )
  );
于 2012-06-19T11:06:01.333 回答