POST一个nsdictionary的NSArray到php


POST a NSArray of NSDictionaries to php

我正在尝试从我的ios应用程序发送nsdictionary的NSArray到我的php webservice,并且在接收特定字符串字段时遇到问题。

我是这样创建POST参数的:

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

"storeId"answers"userId"是字符串,我在php中接收它们没有问题,但"self"。orderItems"是nsdictionary的一个NSMutableArray这就是我遇到的问题。

self中的nsdictionary。orderItems的键为:"ITEM_ID"answers"ITEM_PRICE"。

这是我的代码张贴我的"orderInformation"到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];

我的php中接收这个请求的方法如下:

//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

我已经尝试了这两种方法来迭代$orderItems,但两者都没有获取正确的结果。由于某些原因,ITEM_ID显示为空,但ITEM_PRICE正在获取正确的值。

方法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
       }

方法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
    }

我将$orderStatus返回ios到NSLog,以确保$itemId和$itemPrice被正确检索:

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

但在这两种方法中,$itemId显示为"null",但$itemPrice显示正确的值。我知道item id是我发送的参数,因为我在发送到php之前在ios中记录了它们,它们在那时正确显示。

你能让我知道,如果我错过了什么/不做事情的正确方式,而接收/迭代nsdictionary的NSArray在php?。从早上起我就对这东西着迷。我知道这一定是一件小事/或者我一定错过了一些非常基本的东西,但我似乎无法弄清楚。非常感谢您的回复。

编辑:

从php nlogging中接收到的JSON输出:

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

谢谢,迈克!

您的item_id输出"null",这就是为什么php将其读取为"null"。也许你应该检查你的NSDictionary,看看为什么它输出一个空项。

您正在发送JSON,但未接收JSON。在你的PHP中,你必须这样做:

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

要以您期望的方式发送到PHP,您需要在post body中传递kvp,其中value是url编码的数据:

key1=value1&key2=value2&key3=value3