I am trying to send an array of strings from Objective C to PHP. I was using GET initially but I got to know that GET is not suitable for sending large amount of data. My array would have approximately 300 entries. Therefore, I am using POST. After going through this page I came up with the following code
NSDictionary *userconnections = [user objectForKey:@"connections"];
NSMutableArray *connectionsID = [[NSMutableArray alloc] init];
for (id foo in [userconnections objectForKey:@"values"]) {
[connectionsID addObject:[foo objectForKey:@"id"]];
//[User sharedUser].connections = connectionsID;
}
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:connectionsID options:NSJSONWritingPrettyPrinted error:&error];
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
[User sharedUser].connections = jsonData;
[User sharedUser].connectionsID = jsonString;
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://localhost:8888/API.php"]];
[request setHTTPMethod:@"POST"];
[request setValue:[[User sharedUser]connectionsID] forKey:@"songs"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:[NSString stringWithFormat:@"%lu", (unsigned long)[[[User sharedUser] connections] length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody: [[User sharedUser] connections]];
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSLog(@"Return DATA contains: %@", [NSJSONSerialization JSONObjectWithData:returnData options:NSJSONReadingMutableContainers error:nil]);
In my PHP, I receive it like:
<?php
header("Content-Type: application/json");
$headers = array_change_key_case(getallheaders());
include "dbconnect.php";
$songs = json_decode(stripcslashes($_GET["songs"]));
echo json_encode($songs);
However, I am not able to receive data in php. Any help or pointers would be appreciated
EDIT: I switched back to using GET since POST was not helping me. Thank you for any help you may offer