这在命令行上可以完美地工作:
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>"));
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句