簡體   English   中英

如何將此 cURL 代碼轉換為 PHP(PayPal API)

[英]How to Transform this cURL code to PHP (PayPal API)

我有以下代碼

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 標頭,因此 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);

您可以使用json_decode為您完成腿部工作。

只需將 cURL 傳回給您的字符串提供給它,它就會將您的 JSON 轉換為 php 可用數組。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM