PHP PayPal REST API授权调用

时间:2014-08-23 15:03:56

标签: php api rest curl paypal

我正在尝试使用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和秘密,他们没事。

有人知道我使用的是错误的语法吗?

由于

1 个答案:

答案 0 :(得分:0)

您使用了 curl_setopt($ ch,CURLOPT_USRPWD,&#39; xxxx:xxxx&#39;)而不是 curl_setopt($ ch,CURLOPT_USERPWD,&#39; XXXX: XXXX&#39;); 。请注意错过的&#39; E&#39; ,除此之外,您还需要再添加一行:      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);