Scott T Rogers
Scott T Rogers

Reputation: 545

$_POST data from iOS getting lost when calling PHP web service

I am only a beginning stage iOS developer, but I am a full time PHP developer. My iOS developer's application calls my web service over POST, but for whatever reason, my web service is not receiving the response properly.

Here is the Objective-C code that he has developed:

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
NSData* requestData = [NSData dataWithBytes:[@"{\"method\":\"referrals\",
\"params\":
[{\"iv\":\"some iv value\",
\"data\":
{\"first_name\":\"pEBquIatFcWCrgEVHFhzhw==\",
\"cellular_ipv4\":\"ypMBZu80ZIeVrTpEH0daTg==\",
\"last_name\":\"353YvmRmSw9sKQ+lBrKKcg==\",
\"email\":\"tTiWcT14kuVVgmKuu7PoIw==\",
\"sent_via\":\"69JPdtxeHCAdG/cECGYg5A==\"}
}],
\"id\":\"1\"}" UTF8String] length:[reqString length]];


[request setHTTPMethod:@"POST"];
[request setValue:[NSString stringWithFormat:@"%d", [requestData length]] forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody: requestData];


if (urlConnection != nil) {
[urlConnection release];
urlConnection = nil;
}


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

As far as we can tell, it is sending the data via POST attached to the key 'referrals'. However, when my PHP web service checks the $_POST variable for the 'referrals' key, it does not find it. Here is the relevant PHP code:

if(array_key_exists('referrals', $_POST))
{
    //some code
}else
{
    //error response
}

// Send JSON response
//header('Content-type: application/json');
echo $message;

The iOS app consistently gets back the error response, which is only set if 'referrals' does not exist in $_POST. I've Unit Tested the PHP web service using CURL and get successful results consistently.

Where does the error lie?

Upvotes: 2

Views: 1486

Answers (3)

Scott T Rogers
Scott T Rogers

Reputation: 545

It turns out that the PHP code was not the issue, but the method he was using to send the request over POST. I was never able to find out from him definitively what the specific issue was as he had to rewrite it several times in order to get it to work. From my understanding, the Objective-C way of sending POST data had to be tweaked.

Upvotes: 0

Joelmob
Joelmob

Reputation: 1106

What URL are you trying this on? If you are running trying this on an emulator and your PHP scripts is on localhost it might not work if you try to access a url like http://localhost/myscript.php this is because the emulator might have it's own localhost if it is a virtual machine

In a Android emulator http://10.0.2.2 is the address to the host machine

Upvotes: 0

Michael Frederick
Michael Frederick

Reputation: 16714

Shouldn't your PHP line be this instead?

if(array_key_exists('method', $_POST))

You should debug this on the server side. For example, you could write the contents of var_dump($_POST) to a file.

Upvotes: 2

Related Questions