1

I've been trying to figure this out all morning. I'm sending a POST request from an Objective-C method to a local server on my Mac, using MAMP. When the code runs, the Objective-C method appears to connect successfully, but nothing is received by my PHP script. I rewrote my send method according to this answer, so it the sending should be correct. I've gone through 10-15 similar questions on SO with no luck. My guess now is that something's wrong with the URL, but I can't find the problem. If someone could help me solve this, that would be great.

Here's my code:

IP address: 10.10.2.143

PHP script address: http://localhost:8888/hello_world.php

Objective C:

- (void)hasToken:(STPToken *)token
{
    NSLog(@"Received token %@", token.tokenId);

    NSString *post = [NSString stringWithFormat:@"stripeToken=%@", token.tokenId];
    NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding];
    NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setURL:[NSURL URLWithString:@"http://10.10.2.143:8888/hello_world.php"]];
    [request setHTTPMethod:@"POST"];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [request setHTTPBody:postData];
    NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];

    if (conn)
    {
        NSLog(@"Connection successful!");
    }
    else
    {
        NSLog(@"Connection failed...");
    }
}

PHP:

<?php
    echo "Hello, World!";
    echo $_POST;
?>

Output:

// NSLog
2014-06-16 12:19:45.428 PayPhone Prototype[6519:60b] Received token tok_104EMf4h7nUnb2nUWKejveb9  
2014-06-16 12:19:45.430 PayPhone Prototype[6519:60b] Connection successful!

// PHP
Hello, World!Array
Community
  • 1
  • 1
Impossibility
  • 954
  • 7
  • 20

1 Answers1

1

It seems that you are missing a call to the start method in NSURLConnection… just call

[conn start];

right after recreating the connection object.

One more thing, the way you are handling the success/fail case does not really make sense, since it only checks that the connection object was created (or not), not that the connection was successful:

if (conn)
{
    NSLog(@"Connection successful!");
}
else
{
    NSLog(@"Connection failed...");
}

You should correctly implement your connection object delegate methods: – connectionDidFinishLoading: – connection:didReceiveData: – connection:didFailWithError: (as it is mentioned in the question you are linking to).

Community
  • 1
  • 1
sergio
  • 68,819
  • 11
  • 102
  • 123
  • Thanks, just added the `[conn start]` method in, no luck though. Am I missing something altogether with the PHP? Do I need a `while` loop or something to keep checking if the data has been sent? – Impossibility Jun 16 '14 at 07:30
  • if you implement the delegate's method, especially the `didFailWithError` you can get some more info… also, why don't you try first with a GET (just to troubleshoot things more easily). A GET request is easier to build... – sergio Jun 16 '14 at 07:38
  • The data is definitely being sent and received, the problem is with my PHP webpage not refreshing. I asked a new question: http://stackoverflow.com/questions/24279215/sending-post-request-to-local-host-data-being-received-but-not-showing-up-on-we – Impossibility Jun 18 '14 at 07:14