cURL可以在cli上工作,但不能在php中工作

cURL可以在cli上工作,但不能在php中工作,php,curl,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\/

这在命令行上非常有效:

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."}]}'
curl-vhttps://api.sandbox.paypal.com/v1/payments/payment \
-H“内容类型:应用程序/json”\
-H“授权:持票人”\
-d“{”intent:“sale”,“redirect\u url:“{”return\u url:“https:\/\/devtools paypal.com\/guide\/paypal\/curl?success=true”,“cancel\u url:“https:\/\/devtools paypal.com\/guide\/pay\u paypal\/curl?cancel=true”,“付款人:“{”付款方法:““paypal”,“交易”:“{”金额:{总计:“:“7.47”,“币种:”“美元”,“说明”:“这是付款交易描述。”}]}”
在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>

使用CURLOPT_HTTPHEADER而不是CURLOPT_编码

curl_setopt($ch,CURLOPT_HTTPHEADER,数组(“内容类型:application/json”,“授权:承载”);

您安装了de-php curl扩展吗?是的,我有其他curl脚本运行finecheck
phpinfo()
(或CLI上的
$php-I | grep curl
),并确保安装了curl扩展…如果这不是问题,请检查
curl\u error
curl\u errno
的输出为什么使用CURLOPT\u编码而不是CURLOPT\u HTTPHEADER?@marian0你我的朋友是天使。请发布一个答案,以便我能给你一个代表。
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type:application/json", "Authorization:Bearer <authkey>"));