I have an application written in objective-c which has to send data to a server via a http POST request. The server-side code is written in php. Apparently this code used to work but I cannot seem to integrate it properly. Right now for debugging purposes I am just trying to echo the Post Request back. Here is the important part of the php:
if($_SERVER['SERVER_NAME'] === 'example'){ define('DB_HOST', 'localhost'); } else{ define('DB_HOST', 'example.com'); } print_r($_POST); print_r($_REQUEST); echo 'x'.$HTTP_RAW_POST_DATA; echo 'this is a test'; function getRealPOST() { $result = array(); $b = explode("&", urldecode(file_get_contents("php://input"))); foreach($b as $k => $v) { list($ind, $val) = explode("=", $v); $result[$ind] = $val; } return $result; } $post = getRealPOST() echo "<pre>"; print_r($post); die('here'); Here is the important parts of the objective-c code:
NSMutableDictionary *params = [[NSMutableDictionary alloc] init]; [params setValue:@"goleafsgo" forKey:@"password"]; [params setValue:[NSString stringWithFormat:@"%i", API_VERSION] forKey:@"version"]; [params setValue:uuid forKey:@"uuid"]; [params setValue:@"save_sensor_data" forKey:@"request"]; [params setValue:sensorData forKey:@"sensor_data"]; NSData *jsonData = [NSJSONSerialization dataWithJSONObject:params options:kNilOptions error:nil]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:API_URL cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:20]; [request setValue:@"application/json" forHTTPHeaderField:@"Accept"]; [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; [request setValue:@"json" forHTTPHeaderField:@"Data-Type"]; [request setValue:[NSString stringWithFormat:@"%d", [jsonData length]] forHTTPHeaderField:@"Content-Length"]; [request setHTTPBody:jsonData]; [request setHTTPMethod:@"POST"]; _urlConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; if (_urlConnection) { // Create the NSMutableData that will hold the received data if necessary, otherwise clear it out if (!_responseData) _responseData = [[NSMutableData alloc] init]; } else { NSLog(@"Connection error"); } I have tried several different ways to return the post information from the php but all I get are empty arrays followed by the testString when I use NSLogs. I also used a library to print the NSMutableURLRequest+CurlDescription:
2013-06-27 12:42:54.309 sensimatprototype[3453:907] Request String: Thursday, 27 June, 2013 12:42:54 PM Eastern Daylight Time
Request
curl -X POST -H "Data-Type: json" -H "Content-Length: 61" -H "Content-Type: application/json" -H "Accept: application/json" "http://example.com/api/" -d "{"request":"last_entry","version":"1","password":"examplePassword"}"
When I print out the HTTPResponse allHeaderFields I get this:{ Connection = "keep-alive"; "Content-Length" = 52; "Content-Type" = "text/html"; Date = "Thu, 27 Jun 2013 16:42:53 GMT"; Server = "nginx admin"; "X-Cache" = "HIT from Backend"; "X-Powered-By" = "PHP/5.3.25"; }
I am at a loss. I 'm not even sure how to tell if the problem is on the server-side or the client side. If anyone could point out where I have gone wrong I would really appreciate it.
EDIT I added the getRealPOST function as suggested. I also tried using an HTML form to send data to see what would happen
Here is the form:`
<form action="http://sensimatsystems.com/api" method="post" <input name="pasword" value="test"/> <input name="lastTime" value="test2"/> <input type="submit"> </form> Here is what gets displayed: Array ( ) Array ( [adaptive_image] => 2560 [has_js] => 1 [_utma] => 231368128.879400039.1372276244.1372276244.1372276244.1 [_utmc] => 231368128 [__utmz] => 231368128.1372276244.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none) ) x
Array ( [] => ) here