Paypal API with PHP and cURL

34,305

Solution 1

Having a good trawl around I pieced together parts from developers with other problems. I successfully gained my access token using the following code:

<?php

$ch = curl_init();
$clientId = "myId";
$secret = "mySecret";

curl_setopt($ch, CURLOPT_URL, "https://api.sandbox.paypal.com/v1/oauth2/token");
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_USERPWD, $clientId.":".$secret);
curl_setopt($ch, CURLOPT_POSTFIELDS, "grant_type=client_credentials");

$result = curl_exec($ch);

if(empty($result))die("Error: No response.");
else
{
    $json = json_decode($result);
    print_r($json->access_token);
}

curl_close($ch);

?>

Solution 2

@Lee point the right way but if u are using and old php version it wont work. But Lee version will not show up the error. Use these instead, i only add the error part to see what is going on.

$ch = curl_init();
$clientId = "";
$secret = "";

curl_setopt($ch, CURLOPT_URL, "https://api.sandbox.paypal.com/v1/oauth2/token");
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_USERPWD, $clientId.":".$secret);
curl_setopt($ch, CURLOPT_POSTFIELDS, "grant_type=client_credentials");

$result = curl_exec($ch);
$err = curl_error($ch);

$access_token="";
if ($err) {
  echo "cURL Error #:" . $err;
}
else
{
    $json = json_decode($result);
   // print_r($json->access_token);
    $access_token = $json->access_token;
}

If u are having and old PHP version it is posible it wont work and it will show this error:

cURL Error #:error:14077410:SSL routines:SSL23_GET_SERVER_HELLO:sslv3 alert handshake failure

Just stack overflow it! Here they discuss the problem

Solution 3

JSON and US English appear to be the Defaults, but to be in perfect compliance, add the following line:

curl_setopt($ch, CURLOPT_HTTPHEADER, "Accept: application/json, Accept-Language: en_US");
Share:
34,305
Lee
Author by

Lee

Software Dev.

Updated on October 26, 2020

Comments

  • Lee
    Lee over 3 years

    I'm attempting "the first call" as outlined by the Paypal API documentation. This is the example provided that I'm following:

    curl https://api.sandbox.paypal.com/v1/oauth2/token \
     -H "Accept: application/json" \
     -H "Accept-Language: en_US" \
     -u "EOJ2S-Z6OoN_le_KS1d75wsZ6y0SFdVsY9183IvxFyZp:EClusMEUk8e9ihI7ZdVLF5cZ6y0SFdVsY9183IvxFyZp" \
     -d "grant_type=client_credentials"
    

    I have constructed a curl instance in PHP with all the above headers apart from the last one. What does a -d flag convert to as a curl option in PHP? There is little explanation there as far as I can tell. I managed to deduce -u as CURLOPT_USERPWD.

  • Andy Van Fossen
    Andy Van Fossen over 7 years
    Sadly, it seems as though Paypal has removed their REST API curl samples from Github.
  • Joseph Girgis
    Joseph Girgis over 7 years
    This is perfect saved my life.
  • Lee
    Lee over 7 years
    You probably should have proposed an edit. I'll update my answer to take your error handling in to consideration.
  • Daniel Masih
    Daniel Masih over 6 years
    tnkx buddy you saved my time and life also
  • Tareq Aziz
    Tareq Aziz about 5 years
    Nice and helpful