将iOS字符串发送到PHP
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'];)
我正在尝试让iPhone应用程序将字符串发送到我的Web服务器。 当我在我的php中打印$ _POST时,数组是空的。 p>
这是我目前正在尝试的; p>
// NSString * query = @“SELECT name FROM users”;
NSString * query = @“select = name& from = users”;
NSMutableURLRequest * request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@“http://localhost/www/service.php”]];
[请求setHTTPMethod:@“POST”];
[请求setHTTPBody:[query dataUsingEncoding:NSUTF8StringEncoding]];
[NSURLConnection connectionWithRequest:request delegate:self];
code> pre>
在php脚本中,我只需要执行print_r($ _ POST); 我没有收到错误消息,只有一个空数组。 我究竟做错了什么? 我试过分别访问这两个字段,它也不起作用($ POST ['select'];) p>
div>
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);
if(resSrt)
{
NSLog(@"got response");
}
else
{
NSLog(@"faield to connect");
}
Try using $_REQUEST
. May be $_POST
not works.