Objective-C->JSON->PHP Array

后端 未结 2 1099
暖寄归人
暖寄归人 2020-12-11 09:35

I\'ve been struggling with this for the last few days; I am trying to post an array to PHP. I can successfully send it, but it\'s not taken in with a post-variable (I am try

相关标签:
2条回答
  • 2020-12-11 09:53

    It would be better to use below PHP function response would be in stdobject

    parse_str(file_get_contents("php://input"), $data);
    
    $d = json_decode(json_encode($data));
    
    0 讨论(0)
  • 2020-12-11 10:16

    On the PHP side, I've used something similar to your first example:

    <?php
    
    $handle = fopen("php://input", "rb");
    $http_raw_post_data = '';
    while (!feof($handle)) {
        $http_raw_post_data .= fread($handle, 8192);
    }
    fclose($handle);
    
    // do what you want with it
    //
    // For diagnostic purposes, I'm just going to decode, make sure I got an array, 
    // and respond with JSON that includes status, code, and the original request
    
    $post_data = json_decode($http_raw_post_data,true);
    
    if (is_array($post_data))
        $response = array("status" => "ok", "code" => 0, "original request" => $post_data);
    else
        $response = array("status" => "error", "code" => -1, "original_request" => $post_data);
    
    $processed = json_encode($response);
    echo $processed;
    
    ?>
    

    And then on the iOS side, I use:

    // create the dictionary (or array)
    
    NSDictionary *dictionary = @{@"a": @"One", @"b": @"Two", @"c": @"Three"};
    NSError *error = nil;
    NSData *data = [NSJSONSerialization dataWithJSONObject:dictionary options:0 error:&error];
    if (error)
        NSLog(@"%s: JSON encode error: %@", __FUNCTION__, error);
    
    // create the request
    
    NSURL *url = [NSURL URLWithString:@"your.url.here"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod:@"POST"];
    [request setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    [request setHTTPBody:data];
    
    // issue the request
    
    NSURLResponse *response = nil;
    NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
    if (error)
        NSLog(@"%s: NSURLConnection error: %@", __FUNCTION__, error);
    
    // examine the response
    
    NSString *responseString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
    NSLog(@"responseString: %@",responseString);
    

    I just tested this round trip, and it works fine.


    If you are determined to use the _POST technique, what works for me is to set the the data to be json=%@, such as:

    NSDictionary *dictionary = ...
    NSData *data = [NSJSONSerialization dataWithJSONObject:dictionary options:0 error:&error];
    NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    if (error)
        NSLog(@"%s: JSON encode error: %@", __FUNCTION__, error);
    
    NSURL *url = [NSURL URLWithString:@"your.url.here"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    
    [request setHTTPMethod:@"POST"];
    NSString *params = [NSString stringWithFormat:@"json=%@",
                        [string stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    NSData *paramsData = [params dataUsingEncoding:NSUTF8StringEncoding];
    [request addValue:@"8bit" forHTTPHeaderField:@"Content-Transfer-Encoding"];
    [request addValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [request setHTTPBody:paramsData];
    
    // now send the request, like before
    

    And the PHP to parse it is much like you had:

    $http_raw_post_data = $_POST['json'];
    
    $post_data = json_decode(stripslashes($http_raw_post_data),true);
    
    if (is_array($post_data))
        $response = array("status" => "ok", "code" => 0, "original request" => $post_data);
    else
        $response = array("status" => "error", "code" => -1, "original_request" => $post_data);
    
    $processed = json_encode($response);
    echo $processed;
    
    0 讨论(0)
提交回复
热议问题