0

I'm looking to call a HTTP_POST from the iPhone SDK to a php file on my server. If I call this below:

 NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://*****/api2/index.php"]];

[request setHTTPMethod:@"POST"];
[request addValue:@"postValues" forHTTPHeaderField:@"METHOD"];

//create data that will be sent in the post
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
[dictionary setValue:@2 forKey:@"value1"];
[dictionary setValue:@"This was sent from ios to server" forKey:@"value2"];

//serialize the dictionary data as json
NSData *data = [[dictionary copy] JSONValue];

[request setHTTPBody:data]; //set the data as the post body
[request addValue:[NSString stringWithFormat:@"%d",data.length] forHTTPHeaderField:@"Content-Length"];

NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if(!connection){
    NSLog(@"Connection Failed");
}

php code on server

if ($_SERVER['HTTP_METHOD'] == 'postValues'){ 
$body = $_POST; 
$id;// value1 from dictionary 
$name; // value2 from dictionary
}

Please help with $id and $name

  • Is there a specific reason why you do not stick with the established http methods like "POST"? It just does not seem like the very best practice to invent something new. You newer know what badly programmed proxy will remove/change it. – KillerX Apr 12 '13 at 08:57
  • Actually it can make sense to send json or XML through the post body. Many standards use this approach, such as OAuth and SOAP. Though multipart would work too. – EJTH Apr 12 '13 at 08:59
  • It's a duplicate: [How to get json data from iphone post request](http://stackoverflow.com/questions/3656983/how-to-get-json-data-from-iphone-post-request) (the short answer is that the json data resides in `$body['json']`) And here's another answer with more info: [sending json using nsurlrequest](http://stackoverflow.com/questions/10645317/ios-5-sending-json-using-nsurlrequest-and-parsing-in-php-via-post) – Rikkles Apr 12 '13 at 08:42
  • If you take a look at the code he is supplying he is sending JSON encoded data as the post body. not standard FORM entity encoded data. – EJTH Apr 12 '13 at 08:55
  • edited, it's a duplicate – Rikkles Apr 12 '13 at 09:00
  • Not exactly. Because he asks how he can send additional parameters with the request, i just caught his mistake of assuming that $_POST can be anything but an array – EJTH Apr 12 '13 at 09:01
  • The answer is that he's looking to retrieve his dictionary, which resides in `$body['json']`. What's the difference? After that, he can get whatever variable he wants from it. – Rikkles Apr 12 '13 at 09:03
  • No actually his dictionary would be in `json_decode($body);` – EJTH Apr 12 '13 at 09:05
  • No, in `json_decode($_POST['json'])`, or in his case `json_decode($body['json'])` – Rikkles Apr 12 '13 at 09:06
  • No, please take a look at what he is doing. He is using the POST BODY to send JSON instead of entity encoded form fields. – EJTH Apr 12 '13 at 09:07

2 Answers2

1

First, the method of this request is POST, not postValues. All you did is add a header with the name METHOD and the value postValues, so, if you want to check for that, you need to look into the interface between whatever server you're using and PHP. For Apache, that's apache_request_headers().

Then, if you're setting the JSON object to be the body of the request, then you need to read the body to get to it. To do that, you need to read php://input. So, your example becomes:

$body = json_decode(file_get_contents('php://input'), true);
$id = $body['value1'];// value1 from dictionary 
$name = $body['value2']; // value2 from dictionary
rid
  • 61,078
  • 31
  • 152
  • 193
0

You could either send the extra values in the query (and retrieve using $_GET) Or you could place the values in your json data.

Also the correct way of retrieving your json as an object is:

$bodyAsObject = json_decode( file_get_contents('php://input') );
EJTH
  • 2,178
  • 1
  • 20
  • 25