[英]Empty $_POST and $_FILES, but non empty php://input on multipart/form-data request, iOS AFNetworking library
I am implementing a multipart request on PHP 5.4 (more specifically, using Yii Framework), where 2 of the parameters are plain text and the other one is an image file. 我正在PHP 5.4上实现一个多部分请求(更具体地说,使用Yii Framework),其中两个参数是纯文本,另一个参数是图像文件。 The problem is that if I use the header
Content-Type: multipart/form-data
, when I do the request, I get the following message 问题是,如果我使用标题
Content-Type: multipart/form-data
,则在执行请求时会收到以下消息
Internal Server Error
内部服务器错误
Undefined index: myTextVar1
未定义索引:myTextVar1
Using file_get_contents('php://input')
to check if the server receives the parameters, I can see that all the information has arrived. 使用
file_get_contents('php://input')
检查服务器是否接收到参数,我可以看到所有信息都已到达。 But both $_POST and $_FILES arrays are empty. 但是$ _POST和$ _FILES数组均为空。
If I remove the header Content-Type
, request works fine, $_POST and $_FILES arrays are filled properly. 如果我删除标题
Content-Type
,则请求工作正常,$ Content-Type
POST和$ _FILES数组已正确填充。 I have tried this on Postman Chrome tool, AngularJS and Android, in every case happens the same. 我曾在Postman Chrome工具,AngularJS和Android上尝试过这种方法,但每种情况下都一样。
We are using the library AFNetworking on iOS, which automatically adds the Content-Type header, and we would like to stick to it rather than trying to use another solution. 我们正在使用iOS上的AFNetworking库,该库会自动添加Content-Type标头,我们希望坚持使用它,而不是尝试使用其他解决方案。
Is there any possible way to fix this on PHP or should we try to use an alternative library on iOS? 有什么可能的方法可以在PHP上解决此问题,还是我们应该尝试在iOS上使用替代库?
EDIT: Code of the iOS request 编辑: iOS请求的代码
[sessionManager POST:path parameters:nil constructingBodyWithBlock:^(id formData) {
[sessionManager POST:路径参数:无ConstructingBodyWithBlock:^(id formData){
[formData appendPartWithFormData:[myTextVar1 dataUsingEncoding:NSUTF8StringEncoding] name:@"myTextVar1"]; [formData appendPartWithFormData:[myTextVar2 dataUsingEncoding:NSUTF8StringEncoding] name:@"myTextVar2"]; [formData appendPartWithFileData:dataToUpload name:@"imageAnswer" fileName:@"image.jpg" mimeType:@"image/jpeg"]; } success:^(NSURLSessionDataTask *task, id responseObject) { successBlock(); } failure:^(NSURLSessionDataTask *task, NSError *error) { failureBlock(); }];
We have solved the problem on iOS by using our own implementation of the multipart POST request, with the help of NSMutableURLRequest. 在NSMutableURLRequest的帮助下,我们通过使用自己的多部分POST请求实现来解决了iOS上的问题。 Still this doesn't answer why can't we use AFNetworking or place the Content-Type header on Postman, Android and AngularJS.
但这仍然不能回答为什么我们不能使用AFNetworking或将Content-Type标头放置在Postman,Android和AngularJS上的原因。 We have a strong feeling that the issue with AFNetworking is that sends the request chunked (header
transfer-encoding: chunked
). 我们强烈感觉到AFNetworking的问题是将请求分块发送(标头
transfer-encoding: chunked
)。 We actually send the Content-Type: multipart/form-data
on our custom iOS implementation, so somehow the problem is not that header at all. 实际上,我们在自定义iOS实现中发送了
Content-Type: multipart/form-data
,所以某种程度上问题根本不是那个标头。
On the server side, when using Postman tool while setting Content-Type header, I can verify that data comes through php://input, but does not reach, php://stdin, $_POST and $_FILES. 在服务器端,当使用Postman工具设置Content-Type标头时,我可以验证数据是否通过php:// input到达,但没有到达php:// stdin,$ _ POST和$ _FILES。
We have not tried to disable chunking on Apache, but that might solve the problem too. 我们尚未尝试在Apache上禁用分块,但这也可以解决该问题。
I leave below some related links that can be helpful for others in the future... https://serverfault.com/questions/142512/problem-with-transfer-encoding-chunked-in-apache-2-2/143965#143965 我在下面留下了一些相关的链接,这些链接将来可能对其他人有帮助... https://serverfault.com/questions/142512/problem-with-transfer-encoding-chunked-in-apache-2-2/143965# 143965
https://serverfault.com/questions/59047/apache-sending-transfer-encoding-chunked https://serverfault.com/questions/59047/apache-sending-transfer-encoding-chunked
https://bugs.php.net/bug.php?id=50210 https://bugs.php.net/bug.php?id=50210
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.