如何在PHP中将NSArray传递给索引数组?

如何在PHP中将NSArray传递给索引数组?

问题描述:

I have a simple NSArray in Xcode which I'd like to pass to PHP file and use it as a loop. How can I do this?

For example, my NSArray looks like this:

    NSArray *array = [NSArray arrayWithObjects: @"hello", @"hi", @"how are you", nil];

What I have done so far but no success

    NSArray *array = [NSArray arrayWithObjects:@"hello", @"hi", @"how are you?", nil];
NSError *error;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:array
                                                       options:NSJSONWritingPrettyPrinted error:&error];
    NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];

    NSString *postString = [NSString stringWithFormat:@"array=%@", array];
    NSData* data = [postString dataUsingEncoding:NSUTF8StringEncoding];

    NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];
    NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration: defaultConfigObject delegate:self delegateQueue: [NSOperationQueue mainQueue]];

    NSURL* url = [NSURL URLWithString:@"http://url.com/file.php"];
    NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url
                                                              cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                          timeoutInterval:60.0];
    [urlRequest setHTTPMethod:@"POST"];
    [urlRequest setHTTPBody:data];

    __block NSString *resultString;

    NSURLSessionDataTask* dataTask = [defaultSession dataTaskWithRequest:urlRequest
                                                       completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {

                                                           if(error == nil)
                                                           {
                                                               resultString = [[NSString alloc] initWithData:data encoding: NSUTF8StringEncoding];
                                                               NSLog(@"Data: %@",resultString);
                                                           }


                                                       }];
    [dataTask resume];

My PHP. I went through a tutorial here

    $array = array();
    $array = $_POST['array'];
    $arrlength = count($array);

    for ($i=1; $i <= $arrlength; $i++)
    {
        //do something
    }

I tried echoing the $array but it was empty. I know somethings about Objective-c but I have very little knowledge about PHP. My code can $_POST NSStrings but I'm still confused of how I can do this with NSArray.

If anyone could please give me some pointers or give me links on some tutorials links that will be great. I have tried searching on * and google on how to this, but their wasn't any thing I could understand.

So my question is really simple. How can I convert NSArray to indexed arrays in PHP?

So using what you have and the other question change this:

NSString *postString = [NSString stringWithFormat:@"array=%@", array];

To:

NSMutableString *postString = [NSMutableString alloc]
for (NSString *thing in array) {
    [postString appendFormat:@"array[]=%@&",[thing stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
}

This should give you the post string you want.

You don't just put the JSON in the HTTP body. You should send the correct POST HTTP Format according to RFC 2616. You could check, how JWURLConnection class does it.

EDIT You basically do it like:

array=$_json_string;

Or you red it in PHP like this:

$post = file_get_contents("php://input");
$json_object = json_decode($post, true);