0

I have tried sending POST data to a php file with asynch and synch NSURLConnection and ASIHTTPRequest but both refuse to actually echo anything in $_POST. My s.php file looks like so:

<?php
echo 'Hi There';
echo $_POST['fname'];
foreach ($_POST as $i => $value) {
     echo($i);
}
?>

My ASIHTTRequest is:

NSURL *url = [[NSURL alloc] initWithString:@"http://server.com/s.php"];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setPostValue:@"Ben" forKey:@"fname"];
[request setDelegate:self];
[request setDidFinishSelector:@selector(requestFinished:)];
[request startAsynchronous];

Which only prints the Hi There line when it comes back to requestFinished.

Edit: By using ASIFormDataRequest, I am pretty sure request is set to POST already, at least there doesn't seem to be a way to set it manually. When I tried NSURLConnection I used:

NSURL *url = [NSURL URLWithString:@"http://server.com/s.php"];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:[[NSString stringWithFormat:@"fname=carl"] dataUsingEncoding:NSUTF8StringEncoding]];
NSURLResponse *response;
NSData *urlData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];
NSString *stringResponse = [[NSString alloc] initWithData:urlData encoding:NSASCIIStringEncoding]; 
NSLog(@"%@",stringResponse);

And still only getting the Hi There line, and also getting normal replies when I use an html form.

apchait
  • 51
  • 1
  • 3

2 Answers2

0

I think you have to set some http headers also before posting data, then only php can recognize that the page requested using post method

try with this line

[request setHTTPMethod: @"POST"];

Lavy John
  • 59
  • 7
  • ASIHTTP sets to post automatically, I tried an NSURLConnection with the method set to POST and got the same result as shown in the edit to the question above. I am thinking there is something wrong on the server side? – apchait Jun 16 '11 at 18:54
-1

Try it.

    NSURL *url = [[NSURL alloc] initWithString:@"http://server.com/s.php"];
   ASIHTTPRequest *Asi_request = [ASIHTTPRequest requestWithURL:url];
    [request setPostValue:@"Ben" forKey:@"fname"];
    [request setDelegate:self];
    [request setDidFinishSelector:@selector(requestFinished:)];
    [request startAsynchronous];
[request setHTTPMethod: @"POST"];

//if this will not work then you should generate queue in appledelegate file see:link

may this help you.

GameLoading
  • 6,688
  • 2
  • 33
  • 57