Objective-C和PHP中的POST方法

时间:2011-01-29 03:43:39

标签: php objective-c

我已经通过objective-C

创建了以下POST方法
-(IBAction) postLocation: (id) sender{
 NSString *latitude = @"37.3229978"; 
 NSString *longitude = @"-122.0321823";

 NSMutableURLRequest *request =[NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://www.mydomain.me/webservice.php"]];
 [request setHTTPMethod:@"POST"];

 NSString *post =[[NSString alloc] initWithFormat:@"latitude=%@longitude=%@submit",latitude,longitude];
 [request setHTTPBody:[post dataUsingEncoding:NSUTF8StringEncoding]];

NSURLResponse *response;
        NSError *err;
        NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
        }

我的PHP代码是:

if (isset($_POST['submit']){

  //it doesn't get into this if statement
  $latitude = $_POST['latitude'];
  $longitude = $_POST['longitude'];

}

问题是,为什么PHP代码不能进入if语句?我在哪里可以通过Objective-C声明'submit'?

4 个答案:

答案 0 :(得分:3)

POST的查询字符串不包含submit密钥。您应该将其更改为以下

NSString *post =[[NSString alloc] initWithFormat:@"latitude=%@&longitude=%@&submit=",latitude,longitude];

答案 1 :(得分:2)

参数之间没有“&”。

答案 2 :(得分:2)

-(IBAction) postLocation: (id) sender{

    NSString *latitude = @"37.3229978"; 
    NSString *longitude = @"-122.0321823";

    NSString *post = [NSString stringWithFormat:@"&latitude=%@&longitude=%@", latitude, longitude];

    NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
    NSString *postLength = [NSString stringWithFormat:@"%d",[postData length]];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];

    NSString *address_url = @"http://www.mydomain.me/webservice.php?";

    address_url = [address stringByAppendingString:post];

    [request setURL:[NSURL URLWithString:[NSString stringWithFormat:address]]];
    [request setHTTPMethod:@"POST"];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Current-Type"];
}

答案 3 :(得分:0)

使用if($_SERVER['REQUEST_METHOD'] == 'POST')而不是现在检查表单的代码可能更简单。