0

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]; } 

2 Answers 2

2

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); 
Sign up to request clarification or add additional context in comments.

1 Comment

Thank fot the quick answer, butI never code in PHP, so I ask for help, if they could get the complete code with which I entering mydomine.com/recive.php can see the result.
1
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); ?> 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.