cancel
Showing results for 
Search instead for 
Did you mean: 
Announcements
Pledging our support for Water! Read more

Using PHP curl to create_folder via http

SOLVED
1,512 Views
6 Comments
0 Kudos
Highlighted
Level 3

Using PHP curl to create_folder via http

After successfully using similar methods to get file and account information pulled into a different script, I created the following simple script to create a folder in the dropbox account.

 

	$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($ch, CURLOPT_URL, "https://api.dropboxapi.com/2/files/create_folder");
    curl_setopt($ch, CURLOPT_POSTFIELDS, array('path'=> $folder_name, 'autorename' => 'false'));  
    
    $result = curl_exec($ch);
    $array = json_decode(trim($result), TRUE);
	print_r($array);
	curl_close($ch);

Upon executing, I get no response, no folder is created, and no errors in the logs. What gives? 

1 Accepted solution

Accepted Solutions
Solution
Dropboxer

Re: Using PHP curl to create_folder via http

It looks like your second call to curl_setopt overrides the first, so 'Authorization' doesn't actually get sent.

 

Anyway, here's a modified version of your original code that works for me:

 

$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($ch, CURLOPT_URL, "https://api.dropboxapi.com/2/files/create_folder");
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(array('path'=> $folder_name, 'autorename' => false)));

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

(Curl was changing the Content-Type on you, and you weren't sending up actual JSON.)

 

 

6 Replies
Dropboxer

Re: Using PHP curl to create_folder via http

You certainly should be getting a response or error from this one way or another. I suspect that you're not seeing any output because you're not printing anything out until after you try to json_decode it though. If that method call doesn't work, you won't see anything. I recommend trying to print_r($result) before json_decode to see the actual response you're getting.

Level 3

Re: Using PHP curl to create_folder via http

Hmmm...

 

I did that and I get this error:

 

Error in call to API function "files/create_folder": Bad HTTP "Content-Type" header: "application/json; boundary=------------------------75b6944965de9799". Expecting one of "application/json", "application/json; charset=utf-8", "text/plain; charset=dropbox-cors-hack".

I split up the Header into two lines like so:

 

	curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Bearer ' . $auth_token));
	curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));

And now I get this error:

 

Error in call to API function "files/create_folder": Must provide HTTP header "Authorization" or URL parameter "authorization".

Which doesn't make much sense, because I do provide the Authorization token as the documentation suggests here.

 

Will do some more digging to see if I find anything else. 

Solution
Dropboxer

Re: Using PHP curl to create_folder via http

It looks like your second call to curl_setopt overrides the first, so 'Authorization' doesn't actually get sent.

 

Anyway, here's a modified version of your original code that works for me:

 

$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($ch, CURLOPT_URL, "https://api.dropboxapi.com/2/files/create_folder");
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(array('path'=> $folder_name, 'autorename' => false)));

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

(Curl was changing the Content-Type on you, and you weren't sending up actual JSON.)

 

 

Level 3

Re: Using PHP curl to create_folder via http

Strange thing is I am almost certain I tried that. Guess there was something else missing. Thanks. There was one small change I had to make for my purposes, but it's working perfect! Cheers. slightly smiling face

Level 2

Re: Using PHP curl to create_folder via http

Hi... How to use oauth1.0 authentication in curl php also oauth_signature will autogenerate...

How to solve? 

 

<?php
$credentials = array(
'oauth_consumer_key' => "gdgfdhgfhhh",
'oauth_nonce' => md5(time().rand()),
'oauth_signature_method' => 'HMAC-SHA1',
'oauth_token' => "dfdsfdfsdfsdfge345",
'oauth_timestamp' =>time(),
'oauth_version' => '1.0'
);
ksort( $credentials );
$credentials = $credentials;
$string_params = array();
foreach ( $credentials as $key => $value ) {
$string_params[] = "$key=$value";
}
$signature = "HMAC-SHA1&" . rawurlencode('http://api.mysite.com/wp-json/test-agent-api/v1/order/') . '&' . rawurlencode( implode( '&', $string_params ) );
$hash_hmac_key = rawurlencode('I926Kmy46AHWqxPJWuJUKffIJV4omp5apVdq3audkEkF5gDe' ) . '&' . rawurlencode('CoFEE4Qb2EneSPpoM9WDEyeEK21QpJ67AXPHV9qUaSz2t5So');
$oauth_signature = base64_encode( hash_hmac( 'sha1', $signature, $hash_hmac_key, true ) );
$credentials['oauth_signature'] = $oauth_signature;
$oauth_nonce= wp_generate_password( 12, false, false ); // not sure this is correct or not
$params1 = array(
"aff_id" => "5345",
"tran" => "test",
"prod" => "Currency",
"bcurr" => "US DOLLAR",
"amount" => "1",
"purpo_vis" => "testing"
);
$test=array(
"oauth_consumer_key"=>"gdgfdhgfhhh", // consumer key
"oauth_token"=>"dfdsfdfsdfsdfge345",
"oauth_signature_method"=>"HMAC-SHA1",
"oauth_version"=>"1.0",
"oauth_timestamp"=> time(),
"oauth_nonce"=> $oauth_nonce,
"oauth_signature"=> $credentials['oauth_signature']
);

$params = json_encode($params1);

$ch = curl_init();
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: OAuth ' . $test, 'Content-Type: application/json'));
curl_setopt($ch,CURLOPT_URL,'http://api.mysite.com/wp-json/test-agent-api/v1/order/')
curl_setopt($ch, CURLOPT_POSTFIELDS,$params1);
$result=curl_exec($ch);
if ($result === false)
{
print_r('Curl error: ' . curl_error($crl));
}
curl_close($crl);
print_r($result);
?>
Dropboxer

Re: Using PHP curl to create_folder via http

@tesna It looks like you have a somewhat different question than what was being discussed on this thread, so please open a new thread if you still have any questions, so that we don't spam anyone else on this thread. I don't see anything with Dropbox in your code though, so make sure you share all of the relevant code that you're using for the Dropbox API.

 

For reference though, OAuth 1 is only supported by Dropbox API, which is deprecated. You should migrate to API v2 instead, which uses OAuth 2. We don't have an official PHP SDK for API v2, but you can use a community SDK, or call the HTTP endpoints directly. If you do want to call the endpoints directly, there's a blog post here showing how to use OAuth 2. (It was written for API v1, but it works the same way for API v2.)