簡體   English   中英

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

[英]cURL works on cli but not in php

這在命令行上完美運行:

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>並且沒有任何響應。 為什么會這樣呢?

使用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