0

In my ios app code i'm passing a NSDictionary inside HTTPBody so my php can check if user exist in database.

-(void)checkUser
    {
      NSDictionary *userDict = [NSDictionary dictionaryWithObjectsAndKeys:
      sessionId, @"sessionId",
      sessionName, @"sessionName",
      nil];

      NSData *jsonData = [NSJSONSerialization dataWithJSONObject:userDict
                        options:NSJSONWritingPrettyPrinted error:nil];
      NSString *jsonString = [[NSString alloc] initWithData:jsonData
      encoding:NSUTF8StringEncoding];

      // initialize and open the stream
      NSURL *url = [NSURL URLWithString:@"http://www.mysite.com/userValidate.php"];
      NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
      [request setHTTPMethod:@"POST"];
      [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
      [request setHTTPBody:[jsonString dataUsingEncoding:NSUTF8StringEncoding]];

      [NSURLConnection connectionWithRequest:request delegate:self];
    }

in the PHP file im usig

$raw = file_get_contents('php://input');

and getting back

post = "{\n \"sessionId\" : \"132156\",\n \"sessionName\" : \"My Name\"\n}";

I followed this post https://stackoverflow.com/a/5078426/1333294 and add this method to my PHP file

    function getTestPostData()
    {
     $post_data = explode( "\n", file_get_contents('php://input') );
     $result = array();

     foreach( $post_data as $post_datum ) {
         $pair = explode(":", $post_datum );
         $result[urldecode($pair[0])] = urldecode($pair[1]);
       }
     return $result;
    }

now i'm getting

    explode = {
        " \"sessionId\" " = " \"132156\",";
        " \"sessionName\" " = " \"My Name\"";
        "{" = "";
        "}" = "";
    };

How can I 'clean' the array any further so my arry will be like

        "sessionId" = "132156";
        "sessionName" = "My Name";

Thanks

Community
  • 1
  • 1
ItayAmza
  • 819
  • 9
  • 21

1 Answers1

0

It looks like a json array. Try feeding the string into json_decode()

function getTestPostData()
{
    $post_data = file_get_contents('php://input');
    $result = json_decode( $post_data, true );
    return $result;
}

this can be reduced further, but left using your original variables for clarity.

edit: added 2nd param, to give back an array vs an object

Uberfuzzy
  • 8,253
  • 11
  • 42
  • 50
  • sorry but im new to php ,do I need to do like json_decode($raw = file_get_contents('php://input')); ? – ItayAmza Apr 09 '13 at 11:14
  • my mac decided it's a good time to crush...i'll check it in few minutes, thanks – ItayAmza Apr 09 '13 at 11:21
  • when im using json_decode() method the PHP does not return answer at all, but it does when im not using it, does it mean im not encoding the json properly in my objective c code? – ItayAmza Apr 09 '13 at 11:39
  • forgot you probably need an array, not an object. you need to send a true as the 2nd parameter. updated the code – Uberfuzzy Apr 09 '13 at 12:11