cancel
Showing results for 
Search instead for 
Did you mean: 
Announcements
Update: Find information on Dropbox support during COVID-19 here
Close
cancel
Showing results for 
Search instead for 
Did you mean: 

API v2 with PHP

Squer
Explorer | Level 4

Hello

I'am trying to do a very simple thing with no result. I just try to use the V2 API with PHP cURL. Nb I a m very new on the subject...

According to the documentation I expect to receive the list of the file from my dropbox. But I do not receive anything the var_dump result is false.

 

Does anayone as an Idea? (I hide the $token value)

 

$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Bearer ' . $auth_token, 'Content-Type: application/json'));
curl_setopt($call, CURLOPT_URL, 'https://api.dropboxapi.com/2/file_requests/list');

try{
$result = curl_exec($ch);
} catch (Exception $e) {
echo 'Exception reçue : ', $e->getMessage(), "\n";
}
var_dump($result);

print_r($result);
$array = json_decode(trim($result), TRUE);
print_r($array);
curl_close($ch);

 

1 Accepted Solution

Accepted Solutions

Re: API v2 with PHP

Dropboxer
Dropboxer

1) It looks like there are two issues with this one:

a) You should be supplying the API call parameters as JSON in the body. So, instead of:

$curl_fields = http_build_query($fields);
curl_setopt($ch, CURLOPT_POSTFIELDS,$curl_fields);

You would do something like:

curl_setopt($ch, CURLOPT_POSTFIELDS,json_encode($fields));

b) When identifying just the root folder, you need to just use the empty string "", so instead of:

"path"=> "/",

Do:

"path"=> "",

 

2) I'm just running this code in my terminal. I can't offer help on your development environment itself, so you'll need to investigate how to properly get the error output in your environment. For reference, the Dropbox API returns error information in the response body, which both the `var_dump($result);` and `print_r($result);` in your code are showing me.

View solution in original post

7 Replies 7

Re: API v2 with PHP

Dropboxer
Dropboxer

It looks like there are some issues that would prevent this code from running properly. You should be getting some error output. When I run this, setting my own $auth_token, I get:

 

Warning: curl_setopt() expects parameter 1 to be resource, null given in ...test_266674.php on line 9

That's because you're using an undefined $call instead of $ch here:

curl_setopt($call, CURLOPT_URL, 'https://api.dropboxapi.com/2/file_requests/list');

That should be:

curl_setopt($ch, CURLOPT_URL, 'https://api.dropboxapi.com/2/file_requests/list');

After that, I get:

Error in call to API function "file_requests/list": request body: could not decode input as JSON

You should set the body to fix that:

curl_setopt($ch, CURLOPT_POSTFIELDS, "null");

Moreover, note that the API endpoint /2/file_requests/list you're using "Returns a list of file requests". That refers to the file requests feature. If you want to list the files in the connected account, you should use /2/files/list_folder[/continue] instead.

 

Re: API v2 with PHP

Squer
Explorer | Level 4

Hello

First thing, thanks a lot for your answer.

 

I've tried with 2/files/listfolder but unfortunately I got the same result (var_dump = false)

 

$ch = curl_init();
$fields =array(
"path"=> "/",
"recursive"=> false,
"include_media_info"=> false,
"include_deleted"=> false,
"include_has_explicit_shared_members"=> false,
"include_mounted_folders"=> true) ;

$curl_fields = http_build_query($fields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Bearer ' . $auth_token, 'Content-Type: application/json'));
curl_setopt($ch, CURLOPT_URL, 'https://api.dropboxapi.com/2/files/list_folder');
curl_setopt($ch, CURLOPT_POSTFIELDS,$curl_fields);

try{
$result = curl_exec($ch);
} catch (Exception $e) {
echo 'Exception : ', $e->getMessage(), "\n";
}
var_dump($result);

print_r($result);
$array = json_decode(trim($result), TRUE);
print_r($array);
curl_close($ch);
 
1) Do you  have a idea on what's wrong with my code?
2) How do you get the error message request "body: could not decode input as JSON" ? I do not receive any message unless I test in https://resttesttest.com/ (which doesn t work neither..)
 

Re: API v2 with PHP

Dropboxer
Dropboxer

1) It looks like there are two issues with this one:

a) You should be supplying the API call parameters as JSON in the body. So, instead of:

$curl_fields = http_build_query($fields);
curl_setopt($ch, CURLOPT_POSTFIELDS,$curl_fields);

You would do something like:

curl_setopt($ch, CURLOPT_POSTFIELDS,json_encode($fields));

b) When identifying just the root folder, you need to just use the empty string "", so instead of:

"path"=> "/",

Do:

"path"=> "",

 

2) I'm just running this code in my terminal. I can't offer help on your development environment itself, so you'll need to investigate how to properly get the error output in your environment. For reference, the Dropbox API returns error information in the response body, which both the `var_dump($result);` and `print_r($result);` in your code are showing me.

View solution in original post

Re: API v2 with PHP

tridejur
Explorer | Level 3

I have a problem with create folder, I get empty response from 

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "https://api.dropboxapi.com/2/files/create_folder_v2");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);


$arr = array("path"=>$from, "autorename"=>"false");
$jArr = json_encode($arr);

$arrNull = array("body"=>"");
$jNull = json_encode($arrNull);

curl_setopt($ch, CURLOPT_POSTFIELDS, $jArr);

curl_setopt($ch, CURLOPT_HTTPHEADER, [
"Authorization: Bearer Thetoken",
"Content-Type: application/json"
]);


$result = curl_exec($ch);
echo("res:" .$result);
curl_close ($ch);

Re: API v2 with PHP

Dropboxer
Dropboxer

Hi @tridejur,

It looks like your having a different issue than was originally reported in this thread.  Can you please start a new thread with your code sample.  It will also help us troubleshoot if you include the response you get back from the server.  

Re: API v2 with PHP

tridejur
Explorer | Level 3

Hi sample is on

http://tridejur.uy/t123.php?id=18711573&alta=2018-12-22blank13:15:12%27

Code is ofuscated, but no problem in sharing since i am only using curl calls to your API as per stated on your http API samples.

I need someone to test if the problem is on your side of communication, I do use create folder and upload through your recommendations.

1) Press, createacc.bat(Enter your account)

2) place some files on outputbox

3) press send.bat

Send me a message to 26045165.

 

Re: API v2 with PHP

Dropboxer
Dropboxer

@tridejur You already have an API support ticket open for this that we've been replying to. Please do not post in multiple places for the same issue. We'll continue following up with you on your ticket. Thanks!

Work Smarter with Dropbox

The way we work is changing. Share and discover new ways to work smarter with Dropbox in our community.

Sound good? Let's get started.
Who's talking

Top contributors to this post

What do Dropbox user levels mean?
Need more support?