【问题标题】:How to Transform this cURL code to PHP (PayPal API)如何将此 cURL 代码转换为 PHP(PayPal API)
【发布时间】:2013-12-24 04:30:33
【问题描述】:

我有以下代码

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?

【问题讨论】:

  • 是的,你可以转换它,文档应该会帮助很多:php.net/manual/en/book.curl.php 具体查看curl_setopt 来设置你设置的各种选项。祝你好运。

标签: php api curl paypal


【解决方案1】:

看来您需要这样做。请注意,由于您传递的是 JSON 标头,因此 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);

【讨论】:

    【解决方案2】:

    您可以使用json_decode 为您做腿部工作。

    只需将 cURL 传回给您的字符串输入它,它就会将您的 JSON 转换为 php 可用数组。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-01-16
      • 1970-01-01
      • 1970-01-01
      • 2012-05-10
      • 1970-01-01
      • 2017-05-25
      • 1970-01-01
      相关资源
      最近更新 更多