在iPhone应用程序中使用PHP Web服务

在iPhone应用程序中使用PHP Web服务,php,iphone,objective-c,json,web-services,Php,Iphone,Objective C,Json,Web Services,我用PHP开发了一个Web服务。它使用MySQL数据库。在本文中,我使用了JSON。我想在我的iPhone应用程序中从此Web服务获取数据。我使用了一个只有一个参数的函数。我如何在iPhone中使用它 网络服务: <?php echo getdata($_REQUEST['lastupdate']); function getdata($lastupdatedate){ $json = '{"foo-bar": 12345}'; $obj = json_deco

我用PHP开发了一个Web服务。它使用MySQL数据库。在本文中,我使用了JSON。我想在我的iPhone应用程序中从此Web服务获取数据。我使用了一个只有一个参数的函数。我如何在iPhone中使用它

网络服务:

<?php

echo getdata($_REQUEST['lastupdate']);

function getdata($lastupdatedate){

    $json = '{"foo-bar": 12345}';



    $obj = json_decode($json);

    //print $obj->{'foo-bar'}; // 12345



    $con = mysql_connect("localhost","un","pwd");



    if (!$con)



    {



    die('Could not connect: ' . mysql_error());



    }

    //print_r($con);



    mysql_select_db("roster", $con);







    $query = "select * from rates where LastUpdated = '".$lastupdatedate."' order by LastUpdated limit 1";

    $rs = mysql_query($query) or die($query);

    //print_r($rs);

    while($row=mysql_fetch_assoc($rs)){

    $record[] = $row;

    }



    $data = json_encode($record);



    header('Cache-Control: no-cache, must-revalidate');

    header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');

    header('Content-type: application/json');

    return $data;

}

?>

您将为
http://domain/t1.php?lastupdate=2012-09-01 01:00:00
,然后解析您的响应JSON

根据您的代码应该如何工作(onload或基于某些用户操作),您的代码应该与下面的示例类似

基本代码段:

- (void)viewDidLoad
{
    [super viewDidLoad];

    dispatch_async(kBgQueue, ^{
        NSData* data = [NSData dataWithContentsOfURL:yourURLValue];
        [self performSelectorOnMainThread:@selector(fetchedData:) 
          withObject:data waitUntilDone:YES];
    });
}

- (void)fetchedData:(NSData *)responseData {
    //parse out the json data
    NSError* error;
    NSDictionary* json = [NSJSONSerialization 
        JSONObjectWithData:responseData //1

        options:kNilOptions 
        error:&error];

    NSArray* JSONResultValues = [json objectForKey:@"yourKey"]; //2

    NSLog(@"Results: %@", JSONResultValues); //3
}

我不明白,在url链接中,我应该传递什么?我的url是:01:00:00“,其中2012-09-01 01:00:00是我的输入参数字符串*myvalue=@“2012-09-01 01:00:00”;NSURL*yourURLValue=[[NSURL alloc]initWithString:[NSString stringWIthFormat:@“domain/t1.php?lastupdate=%@”,myvalue]];//您可以创建不同的url,每个url都使用适当的参数值。先生,当我执行NSLog时(价值);