cURL适用于cli但不适用于php

时间:2014-09-14 19:31:14

标签: php curl

这在命令行上完美运行:

curl -v https://api.sandbox.paypal.com/v1/payments/payment \
  -H "Content-Type:application/json" \
  -H "Authorization:Bearer <authkey>" \
  -d '{"intent":"sale","redirect_urls":{"return_url":"https:\/\/devtools-paypal.com\/guide\/pay_paypal\/curl?success=true","cancel_url":"https:\/\/devtools-paypal.com\/guide\/pay_paypal\/curl?cancel=true"},"payer":{"payment_method":"paypal"},"transactions":[{"amount":{"total":"7.47","currency":"USD"},"description":"This is the payment transaction description."}]}'

然后在php中我有以下

    <?php

    $ch = curl_init("https://api.sandbox.paypal.com/v1/payments/payment");

    curl_setopt($ch, CURLOPT_ENCODING, "Content-Type:application/json");
    curl_setopt($ch, CURLOPT_ENCODING, "Authorization:Bearer <authkey>");
    curl_setopt($ch, CURLOPT_POSTFIELDS, '{"intent":"sale","redirect_urls":{"return_url":"https:\/\/devtools-paypal.com\/guide\/pay_paypal\/curl?success=true","cancel_url":"https:\/\/devtools-paypal.com\/guide\/pay_paypal\/curl?cancel=true"},"payer":{"payment_method":"paypal"},"transactions":[{"amount":{"total":"7.47","currency":"USD"},"description":"This is the payment transaction description."}]}');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    $res = json_decode(curl_exec($ch));
    curl_close($ch);

    ?>
    <pre>
    <?php print_r($res); ?>
    </pre>

然而,这仅打印<pre></pre>并且没有任何回应。为什么会这样?

1 个答案:

答案 0 :(得分:1)

使用CURLOPT_HTTPHEADER代替CURLOPT_ENCODING。

curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type:application/json", "Authorization:Bearer <authkey>"));