POST a NSArray of NSDictionaries to php

1k views Asked by At

I am trying to send an NSArray of NSDictionaries from my ios app to my php webservice and having problems receiving a certain string field.

I form the POST parameters this way:

NSDictionary *orderInformation = [NSDictionary dictionaryWithObjectsAndKeys:self.orderItems,@"ORDER_ITEMS",storeId,@"STORE_ID",userId,@"USERID",nil];

"storeId" and "userId" are strings and I am having no problem receiving them in php but "self.orderItems" is an NSMutableArray of NSDictionaries which is where I am facing a problem.

The NSDictionaries in self.orderItems have keys as: "ITEM_ID" and "ITEM_PRICE".

This is my code to post my "orderInformation" to php:

NSString *webService = @"http://localhost/~testuser/Developer/ReceiveOrder.php?rquest=placeOrder";
NSURL *url = [NSURL URLWithString:webService];
AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL:url];
NSURLRequest *placeOrderRequest = [client requestWithMethod:@"POST" path:webService parameters:orderInformation];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:placeOrderRequest success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON){
    [self orderPlaced:JSON];

}failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON){
    NSLog(@"Request Failed with Error: %@, %@", error, error.userInfo);
}];
[operation start];

The method in my php that receives this request is as below:

//Place Order
public function placeOrder(){
$userId = $_POST['USERID'];
$storeId = $_POST['STORE_ID'];
$orderItems = array();
$itemInfo = array();
$orderItems = $_POST['ORDER_ITEMS'];//This is where I receive my NSArray of NSDictionaries

I have tried both these approaches to iterate over $orderItems but both are not fetching the correct results. For some reason, the ITEM_ID is coming out as null but the ITEM_PRICE is fetching the correct value.

approach 1:

    for ($i=0; $i<count($orderItems); $i++)
       {
         $itemInfo = $orderItems[$i]; //receiving correct value
         $itemId = $itemInfo['ITEM_ID']; //receiving correct value
         $itemPrice = $itemInfo['ITEM_PRICE']; //receiving correct value
         $orderStatus['ITEM_ID'] = $itemId ; //comes out as null
         $orderStatus['ITEM_PRICE'] = $itemPrice; //receiving correct value
       }

approach 2:

    foreach ($orderItems as $itemInfo){
         $itemId = $itemInfo['ITEM_ID']; //receiving correct value
         $itemPrice = $itemInfo['ITEM_PRICE']; //receiving correct value
         $orderStatus['ITEM_ID'] = $itemId ; //comes out as null
         $orderStatus['ITEM_PRICE'] = $itemPrice; //receiving correct value
    }

I return the $orderStatus back to ios to NSLog to make sure $itemId and $itemPrice are retrieved correctly:

    $orderStatusJson = json_encode($orderStatus);
    header('Content-Type: application/json');
    echo $orderStatusJson;

But in both approaches, $itemId is showing up as "null" but $itemPrice is showing the correct value. I know for a fact the item id is present is the parameters I am sending because I log them in ios before sending to php and they show up correctly at that point.

Could you please let me know if I am missing something/not doing things the right way while receiving/iterating over the NSArray of NSDictionaries in php? . I have been going mad with thing since morning. I know this must be something small/or I must be missing something very basic but I can't seem to figure it out. You response is much appreciated.

EDIT:

JSON output received from php NSLogged in ios:

JSON - {
"ITEM_ID" = "<null>";
"ITEM_PRICE" = "3.99";
}

Thanks, Mike!

2

There are 2 answers

4
futureelite7 On

Your item_id is outputting "null", that's why php is reading it as "null". Perhaps you should inspect your NSDictionary to see why it's outputting a null item.

0
djunod On

You are sending as JSON, but not receiving as JSON. In your PHP, you will have to do:

$jsonString = file_get_contents('php://input');
if ($jsonString != "") {
  error_log("[".$jsonString ."]");
  $jsonArray = json_decode($jsonString, true);
  error_log(print_r($jsonArray,true));
}

To send to PHP the way that you were expecting, you will need to pass the kvp in the post body like, where value is url encoded data:

key1=value1&key2=value2&key3=value3