1

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

我以这种方式形成 POST 参数:

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

“storeId”和“userId”是字符串,我在 php 中接收它们没有问题,但“self.orderItems”是 NSDictionaries 的 NSMutableArray,这是我遇到问题的地方。

self.orderItems 中的 NSDictionaries 具有以下键:“ITEM_ID”和“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 显示的是正确的值。我知道存在的项目 id 是我发送的参数,因为我在发送到 php 之前将它们记录在 ios 中,并且它们在那时正确显示。

如果我在 php 中接收/迭代 NSDictionaries 的 NSArray 时遗漏了什么/没有以正确的方式做事,您能否告诉我?. 从早上起我就对这件事发疯了。我知道这一定是小事/或者我必须遗漏一些非常基本的东西,但我似乎无法弄清楚。非常感谢您的回复。

编辑:

从 ios 中的 php NSLogged 接收的 JSON 输出:

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

谢谢,迈克!

4

2 回答 2

0

您以 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,您需要在帖子正文中传递 kvp,其中 value 是 url 编码的数据:

key1=value1&key2=value2&key3=value3
于 2013-05-25T10:52:30.977 回答
0

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

于 2013-03-25T01:19:23.857 回答