Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON-data from iOS into PHP-script

Tags:

json

php

ios

iphone

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 ...?

like image 714
Dominik Seibold Avatar asked Dec 20 '11 14:12

Dominik Seibold


2 Answers

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);
?>
like image 130
Nakkeeran Avatar answered Nov 08 '22 11:11

Nakkeeran


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.

like image 1
Maanas Royy Avatar answered Nov 08 '22 10:11

Maanas Royy