0

I have an app that sends data to a PHP script with the NSURLRequest and NSURLConnexion. What I want is to send a data like "Hello world" with post method, and I want display on my app "Hello world from myPHPScript.php !" but I have no experience on web service and specially n PHP.

Here is my objective-c code which sends the request :

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL

                                URLWithString:@"http://localhost:8888/testNSURL/index.php"]

                                cachePolicy:NSURLRequestUseProtocolCachePolicy

                                timeoutInterval:60.0];

    [request setHTTPMethod:@"POST"];
    NSString *postString = @"myVariable=Hello world !";
    [request setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]];

    NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:request delegate:self];

    if (theConnection) {

        receiveData = [NSMutableData data]; 

}

And this is my PHP code :

<?php
if(isset($_REQUEST["myVariable"])) {
    echo json_encode("Hello from index.php !");
}
else    echo json_encode('Request failed');
?>

My questions are :

  • Is my PHP code correct?
  • How can I catch the echo json_encode("Hello from index.php !"); in my obj-c app?
Edelweiss
  • 621
  • 1
  • 9
  • 24

1 Answers1

0

I personally am using ASIFormDataRequest when posting things to a server. When you get info back from this method you can just do something like this to get the data/string value of all that has been echo'd on the server:

- (void)requestFinished:(ASIHTTPRequest *)aRequest {
    NSData *responseData = [aRequest responseData];
    NSString *responseString = [aRequest responseString];
}

Getting a string from Data information is done like so:

[[NSString alloc] initWithData:responseData encoding:NSASCIIStringEncoding]
Manuel
  • 10,153
  • 5
  • 41
  • 60
  • I used it, but I had a lot of errors / warning with and without the ARC, this is why I used NSURLRequest / NSURLConnexion – Edelweiss Apr 10 '12 at 13:09
  • Oh, never really had any problems with it at all.. Maybe i can help you with those errors? – Manuel Apr 10 '12 at 13:10
  • I also added the way to get a string from nsdata :) – Manuel Apr 10 '12 at 13:11
  • Well, I replaced all stuff that uses ASIHTTPRequest by the code above : / – Edelweiss Apr 10 '12 at 13:16
  • So you want to get the strings that the php server outputs? If so you can use the second piece of code i added. – Manuel Apr 10 '12 at 13:19
  • I will find for ASIHTTPRequest again, and update or make another topic about later, thank for try to help me ! :) – Edelweiss Apr 10 '12 at 13:20
  • But i dont see any code of you starting the sending of the request you built. You should use `[theConnection start];` and get the data in the delegate method that gets called. – Manuel Apr 10 '12 at 13:22