So, I looked at an infinite number of similar problems, but none of them answered what I was looking for and answered it fully, so I hope you can all help me.
I need to transfer the restaurantID array from iOS to a PHP file using POST, or somehow that will work well. I know about ASIHTTPRequest, but I'm looking for something inline, and he abandoned the developer. And finally, I do not want to pass them through the URL, because I do not know how many records there will be.
So here is 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, for this purpose, I checked everything and everything looks good, but from the end of PHP it doesn’t even pick it up.
This is 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 what am I doing wrong? Why am I not getting anything at my PHP end. And, most importantly, can someone explain to me how to send an array via POST. Since I feel that this is my real problem here, I do not understand this enough to solve the problem myself. I do not understand how you can put everything with iOS and pick it up on the PHP side.
I hope all this was clear enough, thanks in advance.
EDIT:
I tried passing the array as a string via the URL, then hacked it, fortunately, it worked ... but I'm just under the URL limit, so I would still like to find another solution. At least now I know that the rest of my code worked as expected.