我有以下代码
curl -v https://api.sandbox.paypal.com/v1/payments/payment \
-H 'Content-Type:application/json' \
-H 'Authorization:Bearer EEwJ6tF9x5WCIZDYzyZGaz6Khbw7raYRIBV_WxVvgmsG' \
-d '{
"intent":"sale",
"redirect_urls":{
"return_url":"http://example.com/your_redirect_url/",
"cancel_url":"http://example.com/your_cancel_url/"
},
"payer":{
"payment_method":"paypal"
},
"transactions":[
{
"amount":{
"total":"7.47",
"currency":"USD"
}
}
]
}'
我一直在尝试转换它,但我不知道该使用哪些参数。
如何将其转换为 PHP?
最佳答案
看来您需要这样做。请注意,因为您传递的是 JSON header ,PayPal(可能)要求将正文数据作为 JSON 发送,而不是形式/值对。如果您要传递一组复杂的数据,您可能会发现将 $data
定义为数组并使用 json_encode($data)
将其转换为CURLOPT_POSTFIELDS
属性。
$header = array();
$header[] = 'Content-type: application/json';
$header[] = 'Authorization: Bearer EEwJ6tF9x5WCIZDYzyZGaz6Khbw7raYRIBV_WxVvgmsG';
$url = 'https://api.sandbox.paypal.com/v1/payments/payment';
$data ='{
"intent":"sale",
"redirect_urls":{
"return_url":"http://example.com/your_redirect_url/",
"cancel_url":"http://example.com/your_cancel_url/"
},
"payer":{
"payment_method":"paypal"
},
"transactions":[
{
"amount":{
"total":"7.47",
"currency":"USD"
}
}
]
}';
//open connection
$ch = curl_init();
//set connection properties
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER,$header);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
关于php - 如何将此 cURL 代码转换为 PHP(PayPal API),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20430397/