1

我有一个用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

数组( [] => )在这里

4

2 回答 2

2

我通过在stackoverflow上运行这篇文章找到了答案:php $_POST empty on form submit。

显然,网址需要包含 "www" 。因此,我将常量文件中的 url 从http://ourwebsite.com/api更改为http://www.ourwebsite.com/api

ETA:在我找到这项工作后,我发现网站发生了变化,这产生了意想不到的后果,即在从 iphone 发送到被 php 读取之间的某个地方将所有的 post 请求变成了 get 请求。

于 2013-06-28T17:08:05.773 回答
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

于 2013-06-27T18:06:11.433 回答