php - 如何将此 cURL 代码转换为 PHP(PayPal API)

标签 php api curl paypal

我有以下代码

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/

相关文章:

php - 具有自定义字段的实体

php - isset() 和 !== null 等价吗?

php - curl 获取不到外部网站

r - 将 CURL 转换为 R

linux - bash 中目录名中包含逗号的 Curl 请求

php - 反向序列化()-jquery

php - 用 php 以人类可读的格式显示 mysql 日期时间?

api - Firebase 通过 REST API PHP CURL 获取数据

validation - 如何验证 youtube 视频 ID?

java - 如何在 Android Retrofit api 中为每个 api 使用 OkHttp 拦截器获取 api 请求和 api 响应时间?