使用PHP将照片上传到Photobucket

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 request 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)

1个回答



我没有意识到multipart / form-data提交不需要是原始url编码。 一旦我停止编码参数,一切都按预期工作。</ p>
</ div>

展开原文

原文

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.

dongqin1167
dongqin1167 嗨,您能分享一下完整的PHP代码吗? 我需要用PHP将图像上传到Photobucket。 谢谢
接近 7 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐