如何使用 PHP 发送带有文件和数据数组的 PUT 请求
我已经在 PHP 中设置了 REST 服务和客户端,但在使用 PUT 时遇到了一些问题。
这是我的情况: 我正在编写一个应接受数据数组和图像的 REST 资源。 REST 资源应该更新现有记录,因此我使用 PUT。我正在使用我编写的 PHP curl 客户端发送数据。所以 - 几乎与您将 HTML 多部分表单发送到 PHP 脚本相同的情况,该脚本执行文件上传并接受一些额外的 POST 字段 - 除了 PUT 和 PHP curl 之外。
到目前为止,我一直在发送PUT 请求类似这样的内容(伪代码):
$a_some_data = array('name' => 'test', 'user_id' => 4);
$body = http_build_query($a_data);
$fh = fopen('php://memory', 'rw');
fwrite($body);
rewind($fh);
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => 'http://myapi/resource/someid',
CURLOPT_PUT => TRUE,
CURLOPT_INFILE => $fh,
CURLOPT_INFILESIZE => strlen($body)
));
curl_exec($ch);
并像这样读取服务器上的数据:
parse_str(file_get_contents('php://input'), $put_data);
..效果很好。
所以现在我想添加一个(二进制)文件到混合中。 - 我如何在客户端实现这个? - 我将如何处理服务器上的文件?
为了进行测试,我设置了一个带有文件输入的 HTML 表单,复制了它发送的原始 multipart/form-data 请求,并尝试在 PUT 请求中使用 curl 将该数据作为文件发送。这种方法可行,但我必须手动解析服务器上的原始数据,我不确定这是最好的主意。或者,我想我可以将文件作为 PUT 请求的正文发送,并在 URL 中添加其他参数作为查询字符串 - 但我想这违背了 PUT REST 资源的要点。
请分享您的想法关于这一点。 谢谢!
I've set up a REST service and client in PHP and I'm having a bit of trouble with PUT.
Here's my situation:
I'm coding a REST resource that should accept an array of data and an image. The REST resource should update an existing record, so I'm using PUT. I'm sending the data with a PHP curl client I wrote. So - pretty much the same situation as if you were sending a HTML multipart form to a PHP script that does a file upload and accepts some additional POST fields - except with PUT and PHP curl..
Up 'till now I've been sending the PUT request something like this (pseudo code):
$a_some_data = array('name' => 'test', 'user_id' => 4);
$body = http_build_query($a_data);
$fh = fopen('php://memory', 'rw');
fwrite($body);
rewind($fh);
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => 'http://myapi/resource/someid',
CURLOPT_PUT => TRUE,
CURLOPT_INFILE => $fh,
CURLOPT_INFILESIZE => strlen($body)
));
curl_exec($ch);
and reading the data on the server like so:
parse_str(file_get_contents('php://input'), $put_data);
..which works just fine.
So now I would like to add a (binary) file into the mix.
- How would I implement this on the client side?
- How would I deal with the file on the server?
For a test I set up a HTML form with a file input, copied the raw multipart/form-data request it sends, and tried sending that data as a file with curl in a PUT request. That kind of works, but I would have to parse the raw data on the server manually, which I'm not sure is the best idea. Alternatively, I guess I could send the file as the body of the PUT request, and add the other parameters in the URL as a query string - but I guess that kind of defies the point of a PUT REST resource..
Please share your thoughts on this.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
除非您的原始版本不够,否则至少还有其他两种方法(因为 libcurl 也应该使用该脚本处理二进制文件)。请注意,您决定如何在接收端接收 PUT 不是卷曲问题,因此我将其排除在此响应之外。
1 - 就像您开始时一样,但提供一个 CURLOPT_READFUNCTION,您可以使用它将数据提供给它将发送的 libcurl。
2 - 使用 CURLOPT_POSTFIELDS (带有字符串)并使其看起来像 POST,然后使用 CURLOPT_CUSTOMREQUEST 将 HTTP 方法更改为“PUT”
There are at least two other ways unless your original version isn't enough (since libcurl should deal just fine with binary files too with that script). Note that how you decide receive the PUT in the receiving end is not a curl issue so I'll leave it out of this response.
1 - Like you started out, but provide a CURLOPT_READFUNCTION with which you feed the data to libcurl that it will send.
2 - Use CURLOPT_POSTFIELDS (with a string) and make it look like a POST, and then you change the HTTP method with CURLOPT_CUSTOMREQUEST to "PUT"