【问题标题】:IOS http POST requests seem to be emptyIOS http POST请求似乎是空的
【发布时间】:2013-07-07 07:39:54
【问题描述】:

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

请求

curl -X POST -H "Data-Type: json" -H "Content-Length: 61" -H "Content-Type: application/json" -H "Accept: application/json" "http://example.com/api/" -d "{"request":"last_entry","version":"1","password":"examplePassword"}"

当我打印出 HTTPResponse allHeaderFields 我得到这个:{ 连接=“保持活动”; “内容长度”= 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>

显示的内容如下:Array () Array ( [adaptive_image] => 2560 [has_js] => 1 [_utma] => 231368128.879400039.1372276244.1372276244.1372276244.1 [_utmc] => 23136882 [__utmz] => 231368128.1372276244.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)) x

数组 ( [] => ) 这里

【问题讨论】:

    标签: php objective-c http-post


    【解决方案1】:

    我通过在 stackoverflow 上浏览这篇文章找到了答案:php $_POST empty on form submission.

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

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

    【讨论】:

      【解决方案2】:

      在服务器端试试这个功能

          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 以其他方式获取发布数据会有所帮助

      【讨论】:

      • 我添加了该代码,结果如下:x
        Array ( [] => )
      猜你喜欢
      • 1970-01-01
      • 2018-11-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-15
      • 2023-04-05
      • 1970-01-01
      • 2011-07-29
      相关资源
      最近更新 更多