【问题标题】:cURL to PHP Translation [closed]cURL 到 PHP 翻译 [关闭]
【发布时间】:2014-04-07 22:58:23
【问题描述】:

有谁知道谁把这个翻译成 PHP

curl -v https://api.sandbox.paypal.com/v1/payments/payment \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer {accessToken}' \
-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"
      }
    }
  ]
}'

我真的无法理解这个。

谢谢!

【问题讨论】:

  • Stackoverflow 不是一个免费的在线转换器,可以从某物转换为其他任何东西。但我看到有人可以创建一个用于在线转换的宠物项目:-D
  • 阅读这个php.net/curl,它会帮助你
  • 我认为 OP 的意思是“如何”。

标签: php curl translation


【解决方案1】:

我假设您是从脚本或命令行运行的。

-H 指的是 Header 选项和 -d 指的是命令的数据部分。

我将您拥有的 JSON 字符串转换为 PHP 关联数组以转换回 JSON 字符串(安全!)。

PHP 代码:

$data = array(
    "intent" => "sale",
    "redirect_urls" => array(
        "return_url" => "http://example.com/your_redirect_url/",
        "cancel_url" => "http://example.com/your_cancel_url/"
    ),
    "payer": array(
        "payment_method" => "paypal"
    ),
    "transactions" => array(
        array(
            "amount" => array({
                "total" => "7.47",
                "currency" => "USD"
            )
        )
    )
);
$data_string = json_encode($data);

$ch = curl_init( "https://api.sandbox.paypal.com/v1/payments/payment" );
curl_setopt_array( $ch, array(
    CURLOPT_CUSTOMREQUEST => "POST",
    CURLOPT_HTTPHEADER => array(
        'Content-Type: application/json',
        'Content-Length: ' . strlen($data_string)),
        'Authorization: Bearer ' . $accessToken      //Need to set $accessToken
    ),
    CURLOPT_POSTFIELDS => $data_string,
    CURLOPT_RETURNTRANSFER => true
));

$result = curl_exec( $ch );   //Make it all happen and store response

cURL manual pagePHP cURL Functions

【讨论】:

  • Thaaaaankss :D 我真的很感激。我一直在尝试自己找到一种方法,但似乎找不到人类可读的教程。 :) 我欠你很多时间!。
  • 快速网络搜索将我带到lornajane.net/posts/2011/posting-json-data-with-php-curl,它似乎在做同样的事情。
  • 对,但没有说明如何将原始 curl 实际转换为 php。那是我的问题。我不知道 -v -h -d 是什么意思。
  • 那么你必须先了解它在 cURL 中的含义(而不是 PHP 扩展)。就像从一种人类语言翻译成另一种人类语言一样;您必须知道某些东西在西班牙语中的含义才能将其翻译成日语。
猜你喜欢
  • 2011-03-30
  • 2017-06-10
  • 1970-01-01
  • 2017-02-22
  • 1970-01-01
  • 2017-12-20
  • 1970-01-01
  • 2017-03-09
  • 1970-01-01
相关资源
最近更新 更多