How can I access json data within a php-script, which it received via http-post? I'm 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 the php-script? In other words, when I call json_decode(...)
in the php-script, what is ...
?
If your are sending your JSON in POST method , It can be received in PHP with the below code
<?php $handle = fopen('php://input','r');
$jsonInput = fgets($handle);
// Decoding JSON into an Array
$decoded = json_decode($jsonInput,true);
?>
The post request when sent using iOS does not works with $_POST. If similar request is issued using js the json in post does works.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With