I'm trying to use the Photobucket API. I'm planning on writing a UI in javascript and then using AJAX calls to the server which will basically act as a proxy to the Photobucket servers. I'm using the server as a proxy so that my Photobucket API consumer_secret isn't out in the open. However, I'm having some trouble uploading photos using PHP. I've been able to get other methods from the API to work including authenticated ones like adding and deleting an album. I've been referencing
http://pic.pbsrc.com/dev_help/WebHelpPublic/Content/Methods/Album/Upload%20Media%20to%20an%20Album.htm
and
http://pic.pbsrc.com/dev_help/WebHelpPublic/Content/Examples/Uploading%20Media.htm
I've never done file upload in PHP but I read some of the posts on here and various other places on the web. Basically I have a test page that has an html form that posts the user, album name, and file to a php script on my server. The form is encoded as multipart/form-data.
When the POST request makes it to the page on my server. My server looks up the tokens for the user sets and sets up the basic oauth parameters. It then does all the necessary things for oauth exactly the same to how it does them in the other methods (the ones where everything works).
I'm excluding the uploadfile parameter from the base string and am using the method POST when generating the oauth signature. For my uploadfile parameter I use the following
$params['uploadfile']= '@'.$_FILES['file']['tmp_name'];
Then I set up the curl request like so
$ch = curl_init($url.$resource);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params); // Note $params is an array
$response = curl_exec($ch);
curl_close($ch);
I've also tried adding in
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible);');
However, after all of this Photobucket comes back and says "Authentication failed signature check failed"
I've looked at my base string and it looks fine. Here is an example. I edited out the actual values for the various tokens:
POST&http%3A%2F%2Fapi.photobucket.com%2Falbum%2Fusername%2Fupload&format%3Djson%26oauth_consumer_key%3D00000%26oauth_nonce%3D1313541553TcVIpWJ18WxEZeXO%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1313541553%26oauth_token%3D000000%26oauth_version%3D1.0%26title%3Dtitle%26type%3Dimage
I don't think its a problem with my base string because as I said it looks fine and I have no problems with any of the other methods I've tried. I think it is a problem with the way curl is sending the data for two reasons.
1) I had a problem with deleting an album at first when I tried to do a delete request, and I got a similar error, however, when I simply did a post re开发者_运维问答quest with the added parameter _method=DELETE
everything worked fine
2) This is the only method I've had problems with and so far it is the only method that requires the data being sent is encoded as multipart/form-data
I would have thought that it was something to do with the way I am assigning the uploadfile parameter, but it doesn't complain about the file only the signature and the documentation said to leave that parameter out of the base string.
Does anyone have any thoughts? Right now I'm thinking that I'm missing some curl_setopt's.
I'd compare my request sent to the example in the reference links but I don't know how to view the body of the Curl request (I can view the headers and it does say multipart/form-data)
I didn't realize that multipart/form-data submissions didn't need to be raw url encoded. As soon as I stopped encoding the parameters everything worked as expected.
精彩评论