Home > front end >  How to write a Dropbox Curl request in PHP Curl?
How to write a Dropbox Curl request in PHP Curl?

Time:06-20

good afternoon! I'm newer in Php programming and I would that someone helps me how to write this refresh token request from Dropbox API in Php Curl. I already read the official documentation about PHP Curl but i didn't achieved to understand it.

The Dropbox API request in Curl (terminal):

curl https://api.dropbox.com/oauth2/token \ 
     -d grant_type=refresh_token \
     -d refresh_token=<REFRESH_TOKEN> \
     -u <APP_KEY>:<APP_SECRET>

Where <APP_KEY>, <APP_SECRET> and REFRESH_TOKEN are credentials gotten from Dropbox.

How do I write this in PHP Curl?

Its will return a json output whit the new Oauth2 Access Token from Dropbox, I want to learn how to handle this output.

CodePudding user response:

For a beginner in PHP Http request, you can use this Curl to PHP Curl translate, Wich takes your Curl request and translate it to PHP code.

https://incarnate.github.io/curl-to-php/

This has worked very well for me.

See how the your Curl command was in PHP

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'https://api.dropbox.com/oauth2/token');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "grant_type=refresh_token&refresh_token=<REFRESH_TOKEN>");
curl_setopt($ch, CURLOPT_USERPWD, '<APP_KEY>' . ':' . '<APP_SECRET>');

$headers = array();
$headers[] = 'Content-Type: application/x-www-form-urlencoded';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}
curl_close($ch);
  • Related