使用 PHP 将照片上传到 Photobucket
我正在尝试使用 Photobucket API。我计划用 javascript 编写 UI,然后使用 AJAX 调用服务器,该服务器基本上充当 Photobucket 服务器的代理。我使用服务器作为代理,这样我的 Photobucket API Consumer_secret 就不会公开。但是,我在使用 PHP 上传照片时遇到了一些问题。我已经能够从 API 获取其他方法,包括经过身份验证的方法,例如添加和删除相册。我一直在引用
和
http://pic.pbsrc.com/dev_help/WebHelpPublic/Content/Examples/Uploading%20Media.htm
我从未在 PHP 中完成过文件上传,但我阅读了一些帖子这里和网络上的其他各个地方。基本上我有一个测试页面,它有一个 html 表单,将用户、专辑名称和文件发布到我的服务器上的 php 脚本。表单被编码为 multipart/form-data。
当 POST 请求到达我的服务器上的页面时。我的服务器查找用户集的令牌并设置基本的 oauth 参数。然后,它为 oauth 执行所有必要的操作,与在其他方法(一切正常的方法)中执行的操作完全相同。
我从基本字符串中排除了 uploadfile 参数,并在生成 oauth 签名时使用 POST 方法。对于我的 uploadfile 参数,我使用以下
$params['uploadfile']= '@'.$_FILES['file']['tmp_name'];
设置curl 请求
$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);
然后我像这样 也尝试添加
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible);');
但是,在所有这些 Photobucket 返回并显示“身份验证失败签名检查失败”之后,
我查看了我的基本字符串,它看起来很好。这是一个例子。我编辑了各种标记的实际值:
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
我不认为我的基本字符串有问题,因为正如我所说,它看起来很好,而且我尝试过的任何其他方法都没有问题。我认为这是curl 发送数据的方式存在问题,原因有两个。
1) 当我尝试执行删除请求时,我首先在删除相册时遇到了问题,但是,当我只是使用添加的参数 _method=DELETE
一切工作正常
2)这是我遇到问题的唯一方法,到目前为止,它是唯一需要将发送的数据编码为多部分/表单数据的方法,
我认为这与我分配 uploadfile 参数的方式,但它并没有抱怨文件,只有签名和文档说将该参数保留在基本字符串之外。
有人有什么想法吗?现在我想我错过了一些curl_setopt。
我会将发送到参考链接中的示例的请求进行比较,但我不知道如何查看 Curl 请求的正文(我可以查看标头,它确实显示了 multipart/form-data)
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
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我没有意识到 multipart/form-data 提交不需要进行原始 url 编码。一旦我停止对参数进行编码,一切都会按预期进行。
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.