我有一个用objective-c 编写的应用程序,它必须通过http POST 请求将数据发送到服务器。服务器端代码是用 php 编写的。显然这段代码曾经可以工作,但我似乎无法正确集成它。现在出于调试目的,我只是想回显 Post Request。这是php的重要部分:
if($_SERVER['SERVER_NAME'] === 'example'){
define('DB_HOST', 'localhost');
}
else{
define('DB_HOST', 'example.com');
}
print_r($_POST);
print_r($_REQUEST);
echo 'x'.$HTTP_RAW_POST_DATA;
echo 'this is a test';
function getRealPOST() {
$result = array();
$b = explode("&", urldecode(file_get_contents("php://input")));
foreach($b as $k => $v)
{
list($ind, $val) = explode("=", $v);
$result[$ind] = $val;
}
return $result;
}
$post = getRealPOST()
echo "<pre>";
print_r($post);
die('here');
这是objective-c代码的重要部分:
NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
[params setValue:@"goleafsgo" forKey:@"password"];
[params setValue:[NSString stringWithFormat:@"%i", API_VERSION] forKey:@"version"];
[params setValue:uuid forKey:@"uuid"];
[params setValue:@"save_sensor_data" forKey:@"request"];
[params setValue:sensorData forKey:@"sensor_data"];
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:params options:kNilOptions error:nil];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:API_URL cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:20];
[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];
[request setHTTPMethod:@"POST"];
_urlConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (_urlConnection) {
// Create the NSMutableData that will hold the received data if necessary, otherwise clear it out
if (!_responseData)
_responseData = [[NSMutableData alloc] init];
}
else {
NSLog(@"Connection error");
}
我尝试了几种不同的方法来从 php 返回帖子信息,但是当我使用 NSLogs 时,我得到的只是空数组,后跟 testString。我还使用了一个库来打印 NSMutableURLRequest+CurlDescription:
2013-06-27 12:42:54.309 sensimatprototype[3453:907] 请求字符串:2013 年 6 月 27 日星期四 12:42:54 PM 东部夏令时间
要求
curl -X POST -H“数据类型:json”-H“内容长度:61”-H“内容类型:应用程序/json”-H“接受:应用程序/json”“http://example.com /api/ " -d "{"request":"last_entry","version":"1","password":"examplePassword"}"
当我打印出 HTTPResponse allHeaderFields 我得到这个:{ Connection = "keep-alive"; “内容长度”= 52;“内容类型”=“文本/html”;日期 =“格林威治标准时间 2013 年 6 月 27 日星期四 16:42:53”;服务器 = "nginx 管理员"; "X-Cache" = "来自后端的命中"; "X-Powered-By" = "PHP/5.3.25"; }
我很茫然。我什至不确定如何判断问题出在服务器端还是客户端。如果有人能指出我哪里出错了,我将不胜感激。
编辑我按照建议添加了 getRealPOST 函数。我还尝试使用 HTML 表单发送数据,看看会发生什么
这是表格:`
<form action="http://sensimatsystems.com/api" method="post"
<input name="pasword" value="test"/>
<input name="lastTime" value="test2"/>
<input type="submit">
</form>
Here is what gets displayed: Array ( ) Array ( [adaptive_image] => 2560 [has_js] => 1 [_ utma] => 231368128.879400039.1372276244.1372276244.1372276244.1 [ _utmc] => 231368128 [__utmz] => 231368128.1372276244.1.1.utmcsr =(直接)|utmccn=(直接)|utmcmd=(无)) x
数组( [] => )在这里