Objective - How to POST a CJSON file to a server for data registration

Asked 2 years ago, Updated 2 years ago, 55 views

Retrieve JSON from server in NSURLSession and
I've even created it to be displayed in the list.

Next, I'm hesitating about sending JSON data to the server.

After retrieving results from DB, send data to the server
What should I do if I send it?
There are multiple pieces of data.

If possible, I will receive it by PHP and process it
If you know how to do it, could you tell me?

On the PHP side,

to the point where you want to process it with php:// input All right.

Thank you for your cooperation.

objective-c php json

2022-09-30 20:30

1 Answers

Depending on your environment and version, this is a typical transmission and PHP receipt.

objective-c

NSSstring* jsonRequest=@"{\"test\":{\"item1\":\"item\"',\"item2\":\"item\"}}";

NSData* requestData= [jsonRequestDataUsingEncoding: NSUTF8 StringEncoding];

NSMutableURLRequest* request = [NSMutableURLRequestRequestWithURL: [NSURLURLWithString:@"Connected URL"] cachePolicy: NSURLRequestUseProtocolCachePolicy timeoutInterval: 60.0];

[request setHTTPMethod:@"POST";
[request setValue:@"application/json" for HTTPHeaderField:@"Accept";
[request setValue:@"application/json" for HTTPHeaderField:@"Content-Type"];
[request setValue: [NSString stringWithFormat: @"%d", (int) [requestData length]] for HTTPHeaderField: @"Content-Length";
request setHTTPBody:requestData;

NSURLResponse* response;
NSError* error;


NSData* result = [NSURLConnection sendSynchronousRequest: request returningResponse: & response error: & error];
if(result!=nil){
    NSSstring*res=[NSSstring alloc] initWithData:result encoding:NSUTF8 StringEncoding];
    NSLog(@"data%@",res);
}

PHP


$json=file_get_contents('php://input');
$obj=json_decode($json);
var_dump($obj);
// Omitted


2022-09-30 20:30

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.