2

我正在构建一个 iOS 应用程序/网络服务。iOS 应用程序将数据发送到 php 脚本,如下所示:

    NSError* error = nil;
    NSURLResponse  *res ponse = nil;

    NSArray * keys = [NSArray arrayWithObjects:@"addr", @"date", @"o_cond", nil];    
    NSArray * values = [NSArray arrayWithObjects:addr, date, o_cond, nil];

    NSDictionary * info = [NSDictionary dictionaryWithObjects:values forKeys:keys];
    NSData* jsonData = [NSJSONSerialization dataWithJSONObject:info options:NSJSONWritingPrettyPrinted error:&error];

    NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://myaddr.com/sendEmail.php"]];
    [req setHTTPMethod:@"POST"];
    [req setHTTPBody:jsonData];

    [NSURLConnection sendSynchronousRequest:req returningResponse:&response error:&error];

php收到了数据:

<?php
    // Get all the posted vars  
    $jsonInput = file_get_contents('php://input');
    $decoded = json_decode($jsonInput, true);

    $body = "Inspection Results for " . $decoded->{'addr'} . "\n\n";
    $body = $body . "Inspection date and time: " . $decoded->{'date'} . "\n\n";
    $body = $body . "Overall condition of asset: " . $decoded->{'o_cond'} . "\n\n"; ?>

但由于某种原因,我永远无法在 php 端接收数据。有谁看到这里出了什么问题?

谢谢

4

1 回答 1

0

尝试$decoded['addr']代替$decoded->{'addr'}. 您告诉 json_decode 返回一个关联数组(第二个参数设置为 true),但您似乎正在尝试访问一个对象。

于 2012-12-18T06:10:36.470 回答