php + curl file upload

254 views
Skip to first unread message

spaghetti

unread,
Jan 21, 2009, 7:56:41 PM1/21/09
to Drop.io API
Hey all-

I've been banging my head against this for a little while and keep
getting a 405 - Not Allowed error when I try to post a file with php/
curl. Curl has been working fine for creating notes & adding comments,
but no luck with files no matter which way I try. Does anyone have a
working example using php/curl to upload files?

Many, many thanks.

Jake Good

unread,
Jan 21, 2009, 9:02:38 PM1/21/09
to spaghetti, Drop.io API
Do you have example code that you're trying to use? Make sure to remove drop urls and API keys... but I think we could all help you better if we saw some code...

Jake
--
Jake Good
http://api.drop.io

spaghetti

unread,
Jan 21, 2009, 9:10:55 PM1/21/09
to Drop.io API
$fp = fopen($file, 'r');
$curl = curl_init($apiUploadURL);

curl_setopt ($curl, CURLOPT_POST, 1);
curl_setopt ($curl, CURLOPT_UPLOAD, 1);
curl_setopt ($curl, CURLOPT_INFILESIZE, $fileSize);
curl_setopt ($curl, CURLOPT_INFILE, $fp);
curl_setopt ($curl, CURLOPT_RETURNTRANSFER,1);
curl_setopt ($curl, CURLOPT_POSTFIELDS, "version=$apiVersion&api_key=
$apiKey&token=$apiToken&drop_name=$apiDropName&file=@$file");

$response = curl_exec ($curl);

curl_close ($curl);

Jake Good

unread,
Jan 22, 2009, 9:59:16 AM1/22/09
to spaghetti, Drop.io API
Do you have the ability to capture the entire HTTP Post request in RAW
format? I'm no PHP expert, though I know some of the other Drop.io API
developers are...

Jake Good
http://api.drop.io

Diego Cena

unread,
Jan 22, 2009, 10:10:50 AM1/22/09
to Drop.io API
Mate,
Just a thought: Maybe you are using a guest token without 'File Creation' privileges.

--
Diego
drop.io fan
http://drop.io/clorinda

Jake Good

unread,
Jan 22, 2009, 10:12:26 AM1/22/09
to Diego Cena, Drop.io API
You know, I had thought of that...  a 405 Not Allowed though is not something we'd throw back if the token was bad... we would be throwing back a 403 Not Authorized.... But we might not even be doing that...

Collin Kreklow

unread,
Jan 22, 2009, 4:43:08 PM1/22/09
to spaghetti, Drop.io API
You are mixing POST and PUT options. You can simplify it - try this:

$curl = curl_init($apiUploadURL);

curl_setopt ($curl, CURLOPT_POST, 1);
curl_setopt ($curl, CURLOPT_RETURNTRANSFER,1);
curl_setopt ($curl, CURLOPT_POSTFIELDS, array (
'version'=>"$apiVersion",
'api_key'=>"$apiKey",
'token'=>"$apiToken",
'drop_name'=>"$apiDropName",
'file'=>"@$file"));

$response = curl_exec ($curl);

curl_close ($curl);

--
Collin Kreklow

spaghetti

unread,
Jan 22, 2009, 8:00:37 PM1/22/09
to Drop.io API
Well that just worked like a charm, Collin. Thank you kindly!

Yow

unread,
Feb 10, 2009, 8:40:50 AM2/10/09
to Drop.io API
I didn't quite get the same effect from the above script. A couple
things where I think mine might be going off is the file variable and
the url. I believe to post an asset you would use this url
http://assets.drop.io/upload. And the file variable would be the path
to the script, which I tried locally and as a full url. But what I
get is "no file was povided". All the other required post fields are
filled. Any pointers?

spaghetti

unread,
Feb 10, 2009, 9:17:14 AM2/10/09
to Drop.io API
This is the more complete script I've been using without any problems:

$apiKey = 'yourKey';
$apiToken = 'yourToken';
$apiDropName = 'yourDropName';
$apiVersion = '1.0';
$apiUploadURL = 'http://assets.drop.io/upload';
$file = '/path/to/file.ext';

$curl = curl_init($apiUploadURL);
curl_setopt ($curl, CURLOPT_POST, 1);
curl_setopt ($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($curl, CURLOPT_POSTFIELDS, array (
'version'=>"$apiVersion",
'api_key'=>"$apiKey",
'token'=>"$apiToken",
'drop_name'=>"$apiDropName",
'file'=>"@$file"));
$response = curl_exec ($curl) or die ("there was a problem with the
upload, please try again");
curl_close ($curl);

echo "File uploaded.";

Yow

unread,
Feb 10, 2009, 9:57:40 AM2/10/09
to Drop.io API
Thanks speghetti this makes sense. Its great to have all the
variables outlined and which curl options to use.
Reply all
Reply to author
Forward
0 new messages