通过 RESTful API 上传文件?
我试图进行 RESTful API 调用以通过 POST 方法上传视频。我所缺少的是,我也不知道编写此类 API 的最佳实践,而且我在互联网上找不到任何资源可供遵循。现在我正在这样做:
我正在 PHP 和 zend 框架( Zend_Rest_Route )中工作。
第一种方法:
在客户端使用 file_get_contents 并使用curl 将其 POST 到 API,在服务器端使用 file_put_contents 写入该数据并发送适当的响应。
第二:
使用Zend_File_Treansfer在服务器端接收文件,并将我的上传api端点地址放入zend_form中,设置方法为post。在这种情况下,文件上传到服务器,但提交表单后,地址栏中的 url 指向 api 服务器,并且永远不会返回到表单。
我这样做对吗?如果不对,请让我知道最佳实践是什么以及如何实现这一目标。
谢谢您的宝贵时间。
I was trying to make an RESTful API call to upload videos through POST method. What I am lacking is that I don't know the best practices for writing this kind of API as well I don't find any resource on the internet to follow. Right now I am doing this:
I am working in PHP and zend framework ( Zend_Rest_Route ).
First approach:
using file_get_contents on client side and POST it to API using curl, and on server side using file_put_contents to write that data and sending an appropriate response.
Second:
using Zend_File_Treansfer to receive file at server side, and putting address of my upload api end point in zend_form with setting method as post. In this case file is uploaded to server, but after submitting the form, the url in address bar points to the api server and never comes back to the form.
Am I doing it right?, if not do let me know what are the best practices and how to accomplish this.
Thank you for your time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这样的事情对我有用:
如果您想发布多个文件,或添加其他多部分参数,也可以很容易地将它们添加到其他边界中。
我在另一篇文章中找到了一些代码,您可能可以在 PHP wiki (http://www.php.net/manual/en/function.stream-context-create.php#90411) 中找到类似的代码。但是......该代码没有正确处理回车+换行,我的服务器立即拒绝了该帖子。此外,旧代码还使用 HTTP 版本 1.0(不重复使用套接字)。使用 HTTP 1.1 时,在发布大量文件时会重新使用套接字。 (这也适用于 HTTPS。)我添加了自己的用户代理 - 如果您欺骗某些服务器认为这是浏览器帖子,您可能需要更改用户代理来欺骗浏览器。
Something like this worked for me:
If you want to post several files, or add other multi-part parameters, it's easy to add these in other boundaries too.
I found some of this code on another post, and you can probably find similar code in the PHP wiki (http://www.php.net/manual/en/function.stream-context-create.php#90411). BUT ... That code was not properly handling the carriage return + line feeds and my server was summarily rejecting that post. In addition, that the older code was also using HTTP version 1.0 -- (which does not re-use sockets). When using HTTP 1.1 sockets are re-used when posting lots of files. (This works with HTTPS too.) I added my own user agent - If your are tricking some server into thinking this is a browser post, you might want to change the user agent to spoof a browser.
您是否尝试过在处理上传的控制器操作末尾添加重定向? (如果不是,您确实应该在发布后进行重定向,这是一种良好的做法)(确保在逻辑执行后进行重定向)。本质上,接收帖子数据的“页面”应该只处理数据,并且您想要返回给用户的有关该帖子操作的任何信息都应该在您重定向到的页面上提供给他们。
[form]--POST-->['post'控制器操作]--redirect (302)-->[包含成功/失败信息的页面]
have you tried adding a redirect to end of your controller action that handles the upload? (if not you really should as its good practice to redirect after post) (make sure you redirect AFTER your logic has executed). In essence the 'page' that receives the post data should just work on the data, and any information you want to return to the user about that post action should be given to them on the page you redirect them to.
[form]--POST-->['post' controller action]--redirect (302)-->[page with success/failure info]