PHP PayPal REST API授权调用



我正在尝试使用PHP和cURL从PayPal请求一个认证令牌来创建支付。

我的代码是:
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.sandbox.paypal.com/v1/oauth2/token");
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Accept: application/json',
    'Accept-Language: en_US'
    ));
curl_setopt($ch, CURLOPT_USRPWD, 'xxxx:xxxx');
curl_setopt($ch, CURLOPT_POSTFIELDS, 'grant_type=client_credentials');
$output = curl_exec($ch);
?>

我得到响应:

curl_setopt() expects parameter 2 to be long, string given in Users/anth/sites/abyss/auth.php on line 11
{"error":"invalid_client","error_description":"Invalid client credentials"}

第11行是:

curl_setopt($ch, CURLOPT_USRPWD, 'xxxx:xxxx');

我已经检查了我的客户端ID和Secret,它们都很好。

有没有人知道如果我使用错误的语法或什么?

谢谢

您使用了curl_setopt($ch, CURLOPT_USRPWD, 'xxxx:xxxx')而不是curl_setopt($ch, CURLOPT_USERPWD, 'xxxx:xxxx');。注意遗漏的'E',除此之外你还需要再添加一行:

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

所以更正后的版本如下:

 $ch = curl_init();
 curl_setopt($ch, CURLOPT_URL, "https://api.sandbox.paypal.com/v1/oauth2/token");
 curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Accept: application/json',
    'Accept-Language: en_US'
    ));
curl_setopt($ch, CURLOPT_USERPWD, 'XXXX:XXXX');
curl_setopt($ch, CURLOPT_POSTFIELDS, 'grant_type=client_credentials');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$output = curl_exec($ch);
print_r($output);

最新更新