Martin Godec
Martin Godec

Reputation: 31

Code for retrieving JSON data in PHP

I have a JSON string in an Objective-C app, and I want to send it to a PHP script on a server.

What PHP code should I use to receive and parse this data?

- (IBAction)send:(id)sender{

NSString *jsonString = [selectedPerson.jsonDictionary JSONRepresentation];


NSLog(@"ESE ES EL JSON %@", jsonString);


NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init    ];
NSString*post = [NSString stringWithFormat:@"&json=%@", jsonStri    ng];
NSData*postData = [post dataUsingEncoding:NSASCIIStringEncoding     allowLossyConversion:NO];

NSLog(@"ESTO ES DATA %@", postData);

[request setURL:[NSURL URLWithSt    ring:@"http://www.mydomine.com/recive.php"]];    
[request setHTTPMethod:@"POST"];    
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-    type"];
[request setHTTPBody:postData];
}

Upvotes: 0

Views: 185

Answers (2)

Suresh Kumar Amrani
Suresh Kumar Amrani

Reputation: 935

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);
?>

Upvotes: 1

user399666
user399666

Reputation: 19879

If you look at:

NSString*post = [NSString stringWithFormat:@"&json=%@", jsonString];

You'll see that the JSON will be contained in a POST variable called json.

receive.php

$json = $_POST['json'];
$decoded = json_decode($json);

Upvotes: 2

Related Questions