2

I am trying to convert this cURL command line call to a PHP script. I've read many articles on it , and have tried several options but none were able to return a result. Here's the command line call:

curl -v 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"

Here is what I have tried so far:

$data = "grant_type=client_credentials";
$url = "https://api.sandbox.paypal.com/v1/oauth2/token";
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_HEADER,true);
curl_setopt($ch,CURLOPT_HTTPHEADER, array("Accept:application/json","Accept-     Language:en_US"));

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_USERPWD, "[$idclient]:[$sekret]");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

$result = curl_exec($ch);

curl_close($ch);

echo $result;
var_dump($result);

Please help me in converting it to a PHP script call.

thank you

4
  • Can you add what you've tried in PHP even if it isn't working? Commented Feb 21, 2016 at 19:56
  • php.net/manual/en/ref.curl.php Commented Feb 21, 2016 at 19:57
  • Possible duplicate of Convert command line cURL to PHP cURL Commented Feb 21, 2016 at 19:59
  • Okay @mkaatman I've added what I have been trying so far. Thanks for your advice. Commented Feb 21, 2016 at 20:14

1 Answer 1

1
$url = 'https://api.sandbox.paypal.com/v1/oauth2/token';

$header_array = array(
    'Accept: application/json',
    'Accept-Language: en_US',
);

$username = 'EOJ2S-Z6OoN_le_KS1d75wsZ6y0SFdVsY9183IvxFyZp';
$password = 'EClusMEUk8e9ihI7ZdVLF5cZ6y0SFdVsY9183IvxFyZp';

$data_string = 'grant_type=client_credentials';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header_array);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string); 
curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);  
$data = curl_exec($ch);
curl_close($ch);

var_dump($data);
Sign up to request clarification or add additional context in comments.

2 Comments

But perhaps you should think about using the PayPal PHP SDK?
Associative arrays won't work with CURLOPT_HTTPHEADER. The array should look like this: ['Accept: application/json', 'Accept-Language: en_US']

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.