ios+php中的json解析

ios+php中的json解析,php,ios,json,web-services,function,Php,Ios,Json,Web Services,Function,我用PHP创建了一个web服务,它有一个函数和一个参数。 我使用phpMyadmin作为后端。 它使用json格式获取数据。 Web服务工作得很好。 我想在我的iPhone应用程序中使用此web服务。 我想传递一个参数。 我也参考了rayWenderlich教程。 但却找不到解决办法。 请帮帮我 这是我的web服务代码: <?php echo getdata($_REQUEST['lastupdate']); function getdata($lastupdatedate){

我用PHP创建了一个web服务,它有一个函数和一个参数。 我使用phpMyadmin作为后端。 它使用json格式获取数据。 Web服务工作得很好。 我想在我的iPhone应用程序中使用此web服务。 我想传递一个参数。 我也参考了rayWenderlich教程。 但却找不到解决办法。 请帮帮我

这是我的web服务代码:

<?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","Password");          

    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;    
}

您可能需要研究使用库。提供一个名为AFHTTPClient的类,该类允许您连接到restful web服务并对其进行简单调用。假设有一个终点,您可以这样做:

NSDictionary *body = [NSDictionary dictionaryWitObject:@"LastUpdated" forKey:@"LastUpdated"];

NSString *fileLoc = [[NSBundle mainBundle] pathForResource:@"URLName" ofType:@"plist"];
NSDictionary *fileContents = [[NSDictionary alloc] initWithContentsOfFile:fileLoc];
NSString *urlLoc = [fileContents objectForKey:@"URL"];
urlLoc = [urlLoc stringByAppendingString:@"?lastupdate=2012-09-01 01:00:00"];    

AFHTTPClient *client = [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:urlLoc]];

[client postPath:@"endpoint" parameters:body success:^(AFHTTPRequestOperation *operation, id responseObject) {
    // Do something with the responseObject

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    // Log the error and proceed

}];  

您可以在此处找到文档:

我了解您试图实现的目标,但您遇到了哪些问题?你说该服务正在运行…是的。但我不知道从ios方面…我应该如何给出url,我应该如何传递函数,将输入作为参数等等…我正在编辑我的问题,在其中我给出了我的试用代码。你为什么要将其作为函数,而不仅仅是一个PHP页面?是的..我需要在我的函数中传递一个参数。我要说的是,你不需要为此显式创建一个PHP函数。。。你可以制作一个PHP页面。。。如果您采用使用函数的方法,那么您应该提取并创建多个函数,以便代码更具可读性。甚至可能想用它创建一个类。先生,我的代码中的网络连接是正确的。但我不知道如何传递函数,我应该给出什么作为url,如何管理1个输入参数:我的链接是:01:00:00请根据我提到的代码给出解决方案?
NSDictionary *body = [NSDictionary dictionaryWitObject:@"LastUpdated" forKey:@"LastUpdated"];

NSString *fileLoc = [[NSBundle mainBundle] pathForResource:@"URLName" ofType:@"plist"];
NSDictionary *fileContents = [[NSDictionary alloc] initWithContentsOfFile:fileLoc];
NSString *urlLoc = [fileContents objectForKey:@"URL"];
urlLoc = [urlLoc stringByAppendingString:@"?lastupdate=2012-09-01 01:00:00"];    

AFHTTPClient *client = [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:urlLoc]];

[client postPath:@"endpoint" parameters:body success:^(AFHTTPRequestOperation *operation, id responseObject) {
    // Do something with the responseObject

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    // Log the error and proceed

}];