简体   繁体   English

cURL在cli上有效,但在php中无效

[英]cURL works on cli but not in php

This works perfectly on the command line: 这在命令行上完美运行:

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."}]}'

and then in php I have the following 然后在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>

However this only prints <pre></pre> and no response whatsoever. 但是,这只会打印<pre></pre>并且没有任何响应。 Why is this happening? 为什么会这样呢?

使用CURLOPT_HTTPHEADER而不是CURLOPT_ENCODING。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM