2014-08-04 09:14
浏览 56


I am trying to make an iPhone app send a string to my web server. At the moment when I print out $_POST in my php, the array is empty.

This is what I am currently trying;

    //NSString *query = @"SELECT name FROM users";
    NSString *query = @"select=name&from=users";

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://localhost/www/service.php"]];
    [request setHTTPMethod:@"POST"];
    [request setHTTPBody:[query dataUsingEncoding:NSUTF8StringEncoding]];

    [NSURLConnection connectionWithRequest:request delegate:self];

In the php script I simply do print_r($_POST); I get no error messages, only an empty array. What am I doing wrong? I have tried accessing the two fields separately and it does't work either ($POST['select'];)

图片转代码服务由CSDN问答提供 功能建议

我正在尝试让iPhone应用程序将字符串发送到我的Web服务器。 当我在我的php中打印$ _POST时,数组是空的。


  // NSString * query = @“SELECT name FROM users”; 
 NSString  * query = @“select = name& from = users”; 
 NSMutableURLRequest * request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@“http://localhost/www/service.php”]];  
 [请求setHTTPBody:[query dataUsingEncoding:NSUTF8StringEncoding]]; 
 [NSURLConnection connectionWithRequest:request delegate:self]; 

在php脚本中,我只需要执行print_r($ _ POST); 我没有收到错误消息,只有一个空数组。 我究竟做错了什么? 我试过分别访问这两个字段,它也不起作用($ POST ['select'];)

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • duanfuxing2212 2014-08-07 08:02

    When you say that print_r prints out an empty array, do you mean that it prints it out an empty array in the browser? If that is the case, don't be concerned - it is not meant to print anything. I was working on something similar and realized that I was debugging it the wrong way.

    When you run your simulator in xcode, your php script will get called and $_POST will have a value. If you print_r at that moment it will temporarily hold the value of your two fields (but there is no way to see this on your browser). If you add some NSLogs and print out the response in xcode, you will see that the array is NOT empty.

    What I am trying to say is - the only way you should be debugging this should be in xcode, do not look at your browser.

    Try this to see your response in the log (this was previously posted - see the link in your first comment);

        NSError *err;
        NSURLResponse *response;
        NSData *responseData = [NSURLConnection sendSynchronousRequest:request      returningResponse:&response error:&err];
        NSString *resSrt = [[NSString alloc]initWithData:responseData encoding:NSASCIIStringEncoding];
        //This is for Response
        NSLog(@"got response==%@", resSrt);
            NSLog(@"got response");
            NSLog(@"faield to connect");
    打赏 评论
  • dsfhd78978 2014-08-04 09:18

    Try using $_REQUEST. May be $_POST not works.

    打赏 评论

相关推荐 更多相似问题