【问题标题】:PayPal API: curl request to PHP cURLPayPal API:对 PHP cURL 的 curl 请求
【发布时间】:2015-12-03 14:31:27
【问题描述】:

我正在尝试使用 cURL 在我的网站上添加 PayPal 付款,我找到了如何获取访问令牌,但我不知道如何将此请求(由 PayPal 开发人员提供)转换为 PHP cURL 以继续付款。

curl -v https://api.sandbox.paypal.com/v1/payments/payment \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer <Access-Token>' \
-d '{
  "intent":"sale",
  "redirect_urls":{
    "return_url":"http://example.com/your_redirect_url.html",
    "cancel_url":"http://example.com/your_cancel_url.html"
  },
  "payer":{
    "payment_method":"paypal"
  },
  "transactions":[
    {
      "amount":{
        "total":"7.47",
        "currency":"USD"
      }
    }
  ]
}'

【问题讨论】:

    标签: php curl paypal


    【解决方案1】:

    应该是这样的(未测试):

    $ch = curl_init('https://api.sandbox.paypal.com/v1/payments/payment');
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json',
    'Authorization: Bearer <Access-Token>'
    ));
    
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    
    $payloadData = '{
      "intent":"sale",
      "redirect_urls":{
        "return_url":"http://example.com/your_redirect_url.html",
        "cancel_url":"http://example.com/your_cancel_url.html"
      },
      "payer":{
        "payment_method":"paypal"
      },
      "transactions":[
        {
          "amount":{
            "total":"7.47",
            "currency":"USD"
          }
        }
      ]
    }';
    
    curl_setopt($ch, CURLOPT_POSTFIELDS, $payloadData);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    
    $result = curl_exec($ch);
    $httpStatusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    

    【讨论】:

    • 打赌他接下来会遇到 CURLOPT_VERIFYPEER 的问题:p
    猜你喜欢
    • 2017-05-25
    • 1970-01-01
    • 2021-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-11
    • 2015-01-20
    • 2016-06-14
    相关资源
    最近更新 更多