cURL到PHP翻译

时间:2014-04-07 22:58:24

标签: php curl translation

有谁知道将这个翻译成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"
      }
    }
  ]
}'

我真的无法绕过这个。

谢谢!

1 个答案:

答案 0 :(得分:1)

我假设你是从脚本或命令行运行它。

-H指的是标题选项和 -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