iOS 5-使用NSURLRequest发送json并通过$ _POST在php中进行解析 [英] iOS 5- sending json using NSURLRequest and parsing in php via $_POST
问题描述
我研究了所有可以找到的类似问题,但似乎没有一个适合我的情况,这很简单.
I have looked at all the similar questions I could find but none seem to have all the right pieces together for my situation, which seems very simple.
- 我正在使用POST创建一个简单的NSURLRequest,并使用NSJSONSerialization类将其主体设置为NSDATA构建.
-
我正在测试一个非常简单的php脚本来捕获该数据.
- I'm creating a simple NSURLRequest using POST and setting its body to an NSDATA build using the NSJSONSerialization class.
I'm testing a very simple php script to capture that data.
如果我使用json_decode(file_get_contents('php://input'),1)在PHP中获取值,则代码可以正常工作.但根本不使用$ _POST
The code works fine if I get the value in PHP using json_decode(file_get_contents('php://input'),1); but not at all using $_POST
出于安全原因,我被告知服务器无法启用file_get_contents()...,所以我正在寻找替代方法.
For security reasons, I'm told the server cannot enable file_get_contents() ... so I'm looking for an alternative.
这是我创建请求的Obj-C代码:
Here is my Obj-C code to create the request:
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://www.techno-mac.org/dev101/jsonForm.php"]];
[request setHTTPMethod:@"POST"];
NSMutableDictionary *jsonDict = [[NSMutableDictionary alloc] init];
[jsonDict setValue:self.userNameField.text forKey:@"userName"];
NSLog(@"Dict: %@",jsonDict);
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:jsonDict options:kNilOptions error:nil];
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSLog(@"JSON String: %@",jsonString);
//[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"json" forHTTPHeaderField:@"Data-Type"];
[request setValue:[NSString stringWithFormat:@"%d", [jsonData length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody: jsonData];
NSLog(@"jsonData: %@",jsonData);
self.postConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:true];
这是运行日志时的控制台日志,在已编码为json数据的字段中输入"test":
Here is the console log when I run it, typing "test" in a field that gets encoded in the json data:
2012-05-17 20:26:19.417 RBWebServices[95037:f803] Dict: {
userName = test;
}
2012-05-17 20:26:19.418 RBWebServices[95037:f803] JSON String: {"userName":"test"}
2012-05-17 20:26:19.418 RBWebServices[95037:f803] jsonData: <7b227573 65724e61 6d65223a 22746573 74227d>
2012-05-17 20:26:19.464 RBWebServices[95037:f803] Received response from server, resetting data length to zero
2012-05-17 20:26:19.465 RBWebServices[95037:f803] Succeeded! Received 12 bytes of data
这是我非常简单的php脚本,可以正常工作:
And here is my very simple php script that works:
$json_post = json_decode(file_get_contents('php://input'),1);
$myArray = $json_post["userName"];
print $myArray;
和不包含的代码(返回数组")
and the code that doesn't (Returns "Array")
$json_post = json_decode($_POST);
print $json_post["userName"];
如果我在php中输入以下内容:
If I enter this in php:
var_dump($_POST)
我收到:
array(0){}
因为第一种情况可行,所以我倾向于认为是$ _POST为空,这是原因...或者是我尝试从中获取某些东西的方式,但它似乎完全是空的.
Because the first scenario works, I tend to think it is the $_POST that is empty which is the cause... or the way I'm trying to get something out of it, but it seems completely empty.
如果使用$ _POST无法做到这一点,也许还有另一种不使用file_get_contents的方法?
If this is impossible using $_POST, perhaps there is another way not to use file_get_contents ?
我根本不是一名PHP专家...对不起,如果这看起来微不足道.
I'm far from being a php expert at all... sorry if this seems trivial.
感谢您的任何建议.
推荐答案
使用以下脚本和curl
进行的快速测试表明,$_POST
仅在使用键/值对时填充.否则,将其视为原始输入.
A quick test using the following script and curl
demonstrated that $_POST
was only populated when using key/value pairs. Otherwise it was considered raw input.
var_dump($_POST);
var_dump(file_get_contents('php://input'));
已执行(原始数据):
curl -X POST -d "whatever" http://jason.local/dev/server.php
输出(空$_POST
):
Output (empty $_POST
):
array(0) {
}
string(8) "whatever"
已执行(键/值对):
curl -X POST -d "data=whatever" http://jason.local/dev/server.php
输出:
array(1) {
["data"]=>
string(8) "whatever"
}
string(13) "data=whatever"
这确实可能是HTTP Spec或PHP内部的一部分.我确定其他人可以验证这一点.尽管如此,快速而肮脏的解决方法是使用键/值对.
This may indeed be part of the HTTP Spec or PHP internals. I'm sure someone else can verify that. Nonetheless, the quick and dirty fix would be to use key/value pairs.
这篇关于iOS 5-使用NSURLRequest发送json并通过$ _POST在php中进行解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!