So I've looked through an endless number of like problems but none of them answered what I was looking for or answered it in a complete manner so hopefully you all can help me out.
I need to pass an array of restaurantID's from iOS to a PHP file using POST or anyway that would work well. I know about ASIHTTPRequest but I'm looking for something built in and it has been abandon by the developer. And lastly, I don't want to pass them through the URL because I don't know how many entries there will be.
So here's what I got so far.
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:theURL]];
[request setHTTPMethod:@"POST"];
NSMutableDictionary *jsonDict = [[NSMutableDictionary alloc] init];
[jsonDict setValue:restaurants forKey:@"restIDs"];
NSLog(@"JSON Dict: %@",jsonDict);//Checking my array here
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:jsonDict options:kNilOptions error:nil];
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSLog(@"JSON String: %@",jsonString); //Checking my jsonString here...
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"json" forHTTPHeaderField:@"Data-Type"];
[request setValue:[NSString stringWithFormat:@"%d", [jsonData length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody: jsonData];
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSLog(@"Return DATA contains: %@", [NSJSONSerialization JSONObjectWithData:returnData options:NSJSONReadingMutableContainers error:nil]);
NSArray *restMenuCount = [NSJSONSerialization JSONObjectWithData:returnData options:NSJSONReadingMutableContainers error:nil];
So, from this end, I've checked everything and it all looks good, but from the PHP end it doesn't even pick it up.
Here's what my PHP file looks like:
$restIDs = $_POST['restIDs'];
echo $restIDs; //Checking to see if it even has anything......but nothing there
for ($i = 0; $i < $restIDs.count; $i++) {
$query = "SELECT * FROM `MenuItems` WHERE rest_id = '$restID'";
$result = mysqli_query($connect, $query);
$number = mysqli_num_rows($result);
$menuNumber[$i] = $number;
}
echo json_encode($menuNumber);
So finally, what am I doing wrong? Why am I not receiving anything on my PHP end. And most of all, can someone explain to me how to send array's via a POST. Because I feel like that's my real problem here, I don't understand it enough to fix the problem myself. I don't understand how you can put everything in from the iOS side and pick it up on the PHP side.
I hope all of this was clear enough, thanks in advance.
EDIT:
I tried passing the array as a string through the URL then exploding it, luckily it worked...but I'm just under the URL limit, so I'd still like to figure out another solution. At least, now I know the rest of my code was working as expected.