这是 PayPal 对其 Permissions API 的命令行 cURL 请求示例:
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
我尝试将上述命令行 cURL 中的凭据、令牌和验证器输入到我的命令行中,并收到了成功的响应。
但是,当我尝试使用我的 PHP 应用程序时,我什至没有收到失败响应。这是我的 php cURL 请求:
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;
}
}
以下是上面代码中传递的变量:
$format = 'NV';
$endpoint = 'https://svcs.sandbox.paypal.com/Permissions/GetAccessToken'
$data = "requestEnvelope%5BerrorLanguage%5D=en_US&token=$requestToken&verifier=$verificationCode"
..当然$requestToken
,$verifierCode
并设置为 PayPal 提供给我的内容。
我怀疑我的问题出在我传递给的 $data 中"CURLOPT_POSTFIELDS"
。格式应该是“NV”(name=value 或 key=value),但是如果您查看 PayPal 示例"requestEnvelope.errorLanguage=en_US"
,这看起来不像是简单的 key=value 对,所以我不确定如何为请求设置格式。
我试过这个:
$requestAsArray = array('requestEnvelope' => array("errorLanguage" => "en_US"), "token" => "$requestToken", "verifier" => "$verificationCode");
$data = http_build_query($requestAsArray);
但仍然没有收到来自 PayPal API 的响应。我还尝试将请求作为 JSON 发送并更改$format
为“JSON”,但 API 需要 name=value/key=value 请求。
这是 PayPal 对其 API 的调用的文档:https ://developer.paypal.com/docs/classic/permissions-service/integration-guide/PermissionsUsing/
任何帮助将非常感激!