doushi9474 2011-08-17 00:50
浏览 19
已采纳

使用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条回答 默认 最新

  • 普通网友 2011-08-24 23:55
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题