1

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!

Mike G
  • 751
  • 3
  • 12
  • 21
  • You should post the JSON output. It's more than likely that the formatting's off. – futureelite7 Mar 24 '13 at 23:54
  • @futureelite7 - posted json output from php – Mike G Mar 25 '13 at 00:03
  • Does it make a difference if you use `NSJSONSerialization`'s [+ dataWithJSONObject:options:error:](http://developer.apple.com/library/ios/documentation/Foundation/Reference/NSJSONSerialization_Class/Reference/Reference.html#//apple_ref/occ/clm/NSJSONSerialization/dataWithJSONObject:options:error:)? That should handle the right encoding for you ;) Then you can post the data object this method creates. – HAS May 25 '13 at 15:10

2 Answers2

0

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.

futureelite7
  • 11,462
  • 10
  • 53
  • 87
  • I am doing an NSLog of the item_id in the dictionary before sending it to the webservice and it is showing up correctly so that is why I am thinking maybe there is a problem in the way I am retrieving the NSArray in php as an associative array and the way I am iterating over the associative array. Thanks. – Mike G Mar 25 '13 at 01:36
  • Then dump the JSON before you send it to the webserver. Most probably it is related to an encoding issue. – futureelite7 Mar 25 '13 at 01:38
  • Sorry I dont get you, when you say dump the JSON, you mean log it? And I am just sending an NSArray and not encoding it to JSON. So you think I should be encoding my NSDictionary to JSON before sending to webservice? – Mike G Mar 25 '13 at 01:40
  • yes I mean log it. I know the library is doing the JSON encoding for you, so NSLog the output of the library and show me what you got. – futureelite7 Mar 25 '13 at 01:41
0

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
djunod
  • 4,876
  • 2
  • 34
  • 28