我有一个用objective-c编写的应用程序,它必须通过http POST请求将数据发送到服务器。服务器端代码是用php编写的。显然这段代码曾经工作,但我似乎无法正确地集成它。现在出于调试目的,我只是想回复Post Post。 这是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返回发布信息,但是当我使用NSLog时,我得到的只是空数组,后跟testString。我还使用了一个库来打印NSMutableURLRequest + CurlDescription:
2013-06-27 12:42:54.309 sensimatprototype [3453:907]索取字符串:2013年6月27日星期四东部夏令时间12:42:54
curl -X POST -H“数据类型:json”-H“Content-Length:61”-H“Content-Type:application / json”-H“Accept:application / json”“{{3} }“-d”{“request”:“last_entry”,“version”:“1”,“password”:“examplePassword”}“
当我打印出HTTPResponse allHeaderFields时,我得到了这个:{ Connection =“keep-alive”; “内容长度”= 52; “Content-Type”=“text / html”; 日期=“星期四,2013年6月27日16:42:53 GMT”; Server =“nginx admin”; “X-Cache”=“来自后端的HIT”; “X-Powered-By”=“PHP / 5.3.25”; }
我不知所措。我甚至不确定如何判断问题是在服务器端还是客户端。如果有人能指出我出错的地方,我会非常感激。
EDIT 我按照建议添加了getRealPOST函数。 我还尝试使用HTML表单发送数据以查看会发生什么
以下是表格:`
<form action="http://sensimatsystems.com/api" method="post"
<input name="pasword" value="test"/>
<input name="lastTime" value="test2"/>
<input type="submit">
</form>
以下是显示的内容:Array()数组([adaptive_image] =&gt; 2560 [has_js] =&gt; 1 [_ utma] =&gt; 231368128.879400039.1372276244.1372276244.1372276244.1 [ _utmc] =&gt; 231368128 [__ utmz] =&gt; 231368128.1372276244.1.1.utmcsr =(直接)| utmccn =(直接)| utmcmd =(无))x
阵 ( [] =&gt; ) 这里
答案 0 :(得分:2)
我通过在stackoverflow上运行这篇文章来找出答案:php $_POST empty on form submission.
显然网址需要包含&#34; www&#34; 。所以我将常量文件中的网址从http://ourwebsite.com/api更改为http://www.ourwebsite.com/api。
ETA:在我发现这项工作之后,我发现网站发生了一些变化,这种变化产生了意想不到的后果,即将所有发布请求转换为从iphone发送到php之间的请求。
答案 1 :(得分:1)
在服务器端尝试此功能
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);
希望以其他方式不使用$ _POST
来获取发布数据