0

I'm trying to include an array in a post to a PHP web service. It doesn't seem to be working and I believe it must be a formatting problem of some kind, but I don't know how to format it. I have the following iOS post string: (I know how to POST, that is not the problem.)

NSString *post = [NSString stringWithFormat:@"&action=post_data&start_time=%f&runners=%@&racename=%@&num_splits=%d", timeInterval, runners /* NSMutableArray */, raceName, numberOfSplits];

"runners" is the NSMutableArray and just passing it this way doesn't not seem to work correctly.

How should I pass an array? The PHP cannot be changed by me and the service is expecting an array. I would pass a JSON object to the service, but that is out of my control.

The PHP is just the following:

$runners = $_POST["runners"]; 
5
  • "The service is expecting an array:" you'll need to be more specific. PHP services rarely expect the same kind of array that NSMutableArray's -description will output. How does the service expect the array to be encoded? You said it's not JSON - is it XML? Some custom string format? Commented Apr 16, 2013 at 1:06
  • The PHP code is just: $runners = $_POST["runners"]; Commented Apr 16, 2013 at 1:07
  • Why don't you convert the array to JSON format or something like that? Makes it better and easier. Commented Apr 16, 2013 at 1:11
  • No, it is not. I guess that is confusing, sorry. I'm not worried about the format of anything besides the array though. I know everything else is correct. Commented Apr 16, 2013 at 1:11
  • And, by the way, the PHP code isn't printing anything.. so you don't get any output from that PHP page.. You should let someone modify it to add the line print_r($runners); for debugging purposes. Commented Apr 16, 2013 at 1:13

2 Answers 2

2

It is not clear to me what the PHP wants to get in that parameter.

If the PHP expects to find an array in $runners, then you need to send a POST query with this content (at least):

runners[]=element1&runners[]=element2&...

This will be translated by PHP into an array

{ 'element1', 'element2', ... }

If you sent instead

runners[key1]=element1&runners[key2]=element2&...

then you would have obtained, in PHP, the same result as if you'd written

$runners = array(
     'key1' => 'element1',
     'key2' => 'element2',
      ...
);

JSON has nothing to do with it, unless PHP is doing a json_decode on $runners. (You said nothing about this being the case, so I assume it isn't).

Sign up to request clarification or add additional context in comments.

4 Comments

What would be Objective-C code, if i need to post query with this content?
You need to pass an array? Look at @AbuYusuf's answer, below (and upvote it if it works for you) - update: that looks as if it sends data in GET. So see also stackoverflow.com/questions/9948242/… for POST (in JSON) and stackoverflow.com/questions/2071788/… for another implementation.
Actually, you provided the basic idea and understanding that the things works. Also, i tried to implement @AbuYusuf's answer, but, unfortunatley it didn't work for me.
Did any of the other links turn out useful? Otherwise you can post another question. Just give me a ping if you do so.
0

Isemi is right. To pass an array as a post variable, you need to create a url string in the following format:

http://myserver.com/test.php?myArray[]=123&myArray[]=456

Here's how I implemented it:

NSArray *arrayWithIDs = [[NSArray alloc] initWithObjects:[NSNumber numberWithInt:123], [NSNumber numberWithInt:456], nil];
NSString *postVarArrayString = @"";
NSString *separator = @"?";
for (int i=0; i<[arrayWithIDs count]; i++) {
    if (i>0) {
        separator = @"&";
    }
    postVarArrayString = [NSString stringWithFormat:@"%@%@myArray[]=%d", postVarArrayString, separator, [[arrayWithIDs objectAtIndex:i] integerValue]];
}

// url
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:
                                   @"http://myserver.com/test.php"
                                   @"%@"
                                   , postVarArrayString]
             ];

NSLog(@"%@", [url absoluteString]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.