简体   繁体   中英

PHP PayPal REST API authorization call

I'm trying to use PHP and cURL to request an auth token from PayPal to create a payment.

my code is:

<?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);

?>

I get the response:

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"}

line 11 is:

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

I have checked my Client ID and Secret and they are fine.

Does anyone know if I am using the wrong syntax or something?

Thanks

You have used the curl_setopt($ch, CURLOPT_USRPWD, 'xxxx:xxxx') instead of curl_setopt($ch, CURLOPT_USERPWD, 'XXXX:XXXX'); . Note the missed 'E' and apart from that you need to add one more line : curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

So the corrected version is below :

 $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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM