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