Чтение данных POST из NSURLSession в php - PullRequest
0 голосов
/ 27 марта 2020

Я отправляю данные, используя NSURLSession, используя следующее. Однако я не получаю никаких данных со страницы php.

NSURL *url = [NSURL URLWithString:@"https://xxx/updates.php"];
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:nil delegateQueue:[NSOperationQueue mainQueue]];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
[request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request addValue:@"application/json" forHTTPHeaderField:@"Accept"];

NSDictionary *postDict = @{@"UserID": @"abc"};
NSData *postData = [NSJSONSerialization dataWithJSONObject:postDict options:0 error:nil];

[request setHTTPBody:postData];

NSURLSessionDataTask *postDataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
    NSLog(@"Data: %@", [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding]);
}];

[postDataTask resume];

Это обновления. php страница:

<?php
    //I have tried
    $data = $_POST['UserID']; //But I know it should arrive in a JSON format?

    //Also tried:
    $data = file_get_contents('php://input');

    //And:
    $data = json_decode(file_get_contents('php://input'));

    echo $data;

    var_dump($_POST); //Gives me array(0) in the NSLog
?>

Для каждого из трех ответов $ data я ничего не получаю в возвращаемом. Var_dump ($ _ POST) предполагает, что данные POST не принимаются. Идеи, где я иду не так?

...