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"]; 
Jordan
  • 2,070
  • 6
  • 24
  • 41
  • "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? – Tim Apr 16 '13 at 01:06
  • The PHP code is just: $runners = $_POST["runners"]; – Jordan Apr 16 '13 at 01:07
  • Why don't you convert the array to JSON format or something like that? Makes it better and easier. – edwardmp Apr 16 '13 at 01: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. – Jordan Apr 16 '13 at 01: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. – edwardmp Apr 16 '13 at 01:13

2 Answers2

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).

LSerni
  • 55,617
  • 10
  • 65
  • 107
  • What would be Objective-C code, if i need to post query with this content? – Asif Bilal Dec 17 '14 at 14:46
  • 1
    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 http://stackoverflow.com/questions/9948242/how-to-upload-an-array-to-a-webservice for POST (in JSON) and http://stackoverflow.com/questions/2071788/iphone-sending-post-with-nsurlconnection for another implementation. – LSerni Dec 17 '14 at 15:12
  • 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. – Asif Bilal Dec 18 '14 at 11:06
  • Did any of the other links turn out useful? Otherwise you can post another question. Just give me a ping if you do so. – LSerni Dec 18 '14 at 17:53
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]);
Ali Saeed
  • 1,519
  • 1
  • 16
  • 23