将命令行cURL请求转换为PHP cURL请求(PayPal API)
下面是PayPal对其权限API的命令行cURL请求示例:将命令行cURL请求转换为PHP cURL请求(PayPal API),php,curl,nvp,Php,Curl,Nvp,下面是PayPal对其权限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_sign
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\u 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);
但仍然没有收到贝宝API的回复。我还尝试将请求作为JSON发送,并将$format
更改为“JSON”,但API需要name=value/key=value请求
以下是PayPal的API调用文档:
任何帮助都将不胜感激 以NVP格式使用标题时
'X-PAYPAL-REQUEST-DATA-FORMAT : NV'
请求数组应该是
$requestAsArray = array(
'requestEnvelope.errorLanguage' =>"en_US"
, "token" => "$requestToken"
, "verifier" => "$verificationCode"
);
$requestAsArray = array(
'requestEnvelope' => array("errorLanguage" => "en_US")
, "token" => "$requestToken"
, "verifier" => "$verificationCode");
然后使用
$data = http_build_query($requestAsArray);
当您使用json格式的标题时
'X-PAYPAL-REQUEST-DATA-FORMAT : JSON'
请求数组应该是
$requestAsArray = array(
'requestEnvelope.errorLanguage' =>"en_US"
, "token" => "$requestToken"
, "verifier" => "$verificationCode"
);
$requestAsArray = array(
'requestEnvelope' => array("errorLanguage" => "en_US")
, "token" => "$requestToken"
, "verifier" => "$verificationCode");
然后使用
$data = json_encode($requestAsArray);
我在Paypal Adaptive Payment呼叫中使用了这种方法,而且它总是有效的。在这种情况下,它也应该起作用。当您使用NVP格式的标题时
'X-PAYPAL-REQUEST-DATA-FORMAT : NV'
请求数组应该是
$requestAsArray = array(
'requestEnvelope.errorLanguage' =>"en_US"
, "token" => "$requestToken"
, "verifier" => "$verificationCode"
);
$requestAsArray = array(
'requestEnvelope' => array("errorLanguage" => "en_US")
, "token" => "$requestToken"
, "verifier" => "$verificationCode");
然后使用
$data = http_build_query($requestAsArray);
当您使用json格式的标题时
'X-PAYPAL-REQUEST-DATA-FORMAT : JSON'
请求数组应该是
$requestAsArray = array(
'requestEnvelope.errorLanguage' =>"en_US"
, "token" => "$requestToken"
, "verifier" => "$verificationCode"
);
$requestAsArray = array(
'requestEnvelope' => array("errorLanguage" => "en_US")
, "token" => "$requestToken"
, "verifier" => "$verificationCode");
然后使用
$data = json_encode($requestAsArray);
我在Paypal Adaptive Payment呼叫中使用了这种方法,而且它总是有效的。在这种情况下也应该有效