1

This is my first question and I hope to get some help:

I've used several methods (and tried with different http transmission frameworks) to make a post action to a php script in localhost with no good results. I don't know what else to look for. I've been stuck with this for at least 6 or 7 hours and finally got to this:

Objective C code:

if (name != nil && message != nil) {
        NSMutableString *postString = [NSMutableString stringWithString:TFPostURL];

        [postString appendString:[NSString stringWithFormat:@"%@=%@",TFName,name]];

        [postString appendString:[NSString stringWithFormat:@"%@=%@",TFMessage,message]];

        [postString setString:[postString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

        NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:postString]];

        [request setHTTPMethod:@"POST"];

        postConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];


    }

TFPostURL, TFName, TFMessage, name and message are constants defined by me.

I'm using the database just to check if the transmission has been successfully done:

PHP Script

<?php

$username = "root";
$database = "db";

mysql_connect(localhost, $username);

@mysql_select_db($database) or die("Unable to find database");

$name = $_POST["name"];

$message = $_POST["message"];

$query = "INSERT INTO test VALUES ('', '$name', '$message')";

mysql_query($query) or die(mysql_error("error"));

mysql_close();

?>

Any ideas on what would be wrong?

I've been tried always in localhost, still need to try this in another mac. I'm on my way to upload my php file to a public domain and check if it's something in my own computer.


Edit:

I've changed the PHP POST action to GET, obtaining amazing results; NOW IT WORKS!

<?php

$username = "root";
$database = "db";

mysql_connect(localhost, $username);

@mysql_select_db($database) or die("Unable to find database");

$name = $_GET["name"];

$message = $_GET["message"];

$query = "INSERT INTO test VALUES ('', '$name', '$message')";

mysql_query($query) or die(mysql_error("error"));

mysql_close();

?>
Jav_Rock
  • 22,059
  • 20
  • 123
  • 164
facandiav
  • 75
  • 10
  • Just so you know, localhost on the simulator should work - Safari on the simulator is able to access my mac's http server. Try opening your php script in the iOS Simulator's Safari browser and see if the database entry pops up there. If it does, then you can be certain it's the app code causing the issue, not the php. – Tim Jan 26 '12 at 04:16
  • Welcome to SO, Fernando! –  Jan 26 '12 at 04:17
  • Thanks guys! I've solved my problem... really, I never saw it coming this way. Check the Edit! – facandiav Jan 26 '12 at 05:56

1 Answers1

3

I recommend using the ASIHTTP libraries. They are excellent. Here's some example code:

ASIHTTPRequest* req = [[[ASIHTTPRequest alloc] initWithURL:[NSURL URLWithString:@"localhost"]] autorelease];
[req setCompletionBlock:^{  
   NSLog(@"It works! Response was %@",[req responseString]);
 }];
[req startAsynchronous];

It makes asynchronous responses a snap and lets you use closures like a boss.

I have tried dealing with the built-in NSURLRequest methods for a while and strongly prefer the simplicity of being able to make asynchronous requests without creating a separate delegate and methods to handle it. It also has support for POSTing data and files, as well as some other tricks I've even yet to use.

Tim
  • 14,447
  • 6
  • 40
  • 63
  • Hi Tim, I've already tried ASIHTTP Libraries with no good results although everyone is using them with no problem. This points that something is strangely wrong. – facandiav Jan 26 '12 at 05:52
  • Are you certain your php code is working? Can you try the code above, but make your webpage simply have a simple string and verify that the simulator is receiving the response? **What ISN'T working?** The request altogether? The values being sent? – Tim Jan 26 '12 at 05:55
  • Used network debugger and things out went fine, started making changes in my php file and as you thought, that was the problem. Simply changed the HTTP POSTs to GETs and went through it. Silly right? – facandiav Jan 26 '12 at 05:58
  • Awesome. Glad to have helped. – Tim Jan 26 '12 at 06:02
  • Please note that ASIHTTPRequest is not actively developed anymore. http://allseeing-i.com/%5Brequest_release%5D – vikingosegundo Jan 26 '12 at 06:10