JSON data from iOS to PHP script

How can I access json data in php-script that it got via http-post? I am doing the following on the iOS side:

NSData *data = [NSJSONSerialization dataWithJSONObject:object options:0 error:NULL];

NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://example.com/script.php"]];
[req setHTTPMethod:@"POST"];
[req setHTTPBody:data];

[NSURLConnection sendSynchronousRequest:req returningResponse:&response error:&err];

How do I access this data in php script? In other words, when I call json_decode(...)in php-script, what is it ...?

+5
source share
4 answers

If you send your JSON in the POST method, you can get it in PHP with the code below

<?php $handle = fopen('php://input','r');
                $jsonInput = fgets($handle);
                // Decoding JSON into an Array
                $decoded = json_decode($jsonInput,true);
?>
+14
source

Mail request when sending using iOS does not work with $ _POST. If a similar request is issued using js, json in post works.

+1
source

php script POST

json_decode($data);

, .

0
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod:@"POST"];
    [request setHTTPBody: jsonData];
    [request setValue:@"text/html" forHTTPHeaderField:@"Content-Type"];
    [request setValue:[NSString stringWithFormat:@"%d", [jsonData length]] forHTTPHeaderField:@"Content-Length"];  

0
source

All Articles