从iOS发布数据到PHP脚本 [英] post data to PHP script from iOS

查看:112
本文介绍了从iOS发布数据到PHP脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试以下代码,用户在字段(title,desc,city)中输入数据后执行,然后单击保存。

I am trying the following code that executes after user enter data in fields (title, desc, city) and then clicks save.

- (IBAction)saveDataAction:(id)sender {
    NSString *lEventTitle = [NSString stringWithFormat:@"var=%@",eventTitle.text];
    NSString *lEventDesc = [NSString stringWithFormat:@"var=%@",eventDescription.text];
    NSString *lEventCity = [NSString stringWithFormat:@"var=%@",eventCity.text];

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://mydomain.com/ios/form.php"]];
    [request setHTTPMethod:@"POST"];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
    //this is hard coded based on your suggested values, obviously you'd probably need to make this more dynamic based on your application's specific data to send

    NSString *postString = [[NSString alloc] initWithFormat:@"title=%@&description=%@&city=%@", lEventTitle, lEventDesc, lEventCity];
    NSString *clearPost = [postString
                                     stringByReplacingOccurrencesOfString:@"var=" withString:@""];
    NSLog(@"%@", clearPost);
    [request setHTTPBody:[clearPost dataUsingEncoding:NSUTF8StringEncoding]];
    [request setValue:clearPost forHTTPHeaderField:@"Content-Length"];
    [NSURLConnection connectionWithRequest:request delegate:self];
    NSLog(@"%@", request);
}

当我NSLog clearPost字符串时(包含要发送到服务器的变量的字符串) ),它显示:

When I NSLog the clearPost string (string containing the variables to be sent to server), it shows:

title=xmas&description=Celebration&city=NY

在PHP端,我的简单如下:

on the PHP end, I have simple as:

<?php
 $title = $_POST['title'];
 $description = $_POST['description'];
 $city = $_POST['city'];

 echo "Title: ". $title;
 echo "Description: ". $description;
 echo "City: ". $city;
?>

只是尝试获取PHP上的数据,以便以后我可以操作它。现在,当我执行

Just trying to get data on PHP so that later I can manipulate it. Right now when I execute the

domain.com/ios/form.php时,它会显示标题,说明和城市空白。

domain.com/ios/form.php, it shows the title, description and city empty.

很抱歉,我对iOS和Objective-C非常陌生。

Sorry but I am very new to iOS and Objective-C.

推荐答案

目标C

// Create your request string with parameter name as defined in PHP file
NSString *myRequestString = [NSString stringWithFormat:@"title=%@&description=%@&city=%@",eventTitle.text,eventDescription.text,eventCity.text];

// Create Data from request
NSData *myRequestData = [NSData dataWithBytes: [myRequestString UTF8String] length: [myRequestString length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString: @"http://www.youardomain.com/phpfilename.php"]];
// set Request Type
[request setHTTPMethod: @"POST"];
// Set content-type
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
// Set Request Body
[request setHTTPBody: myRequestData];
// Now send a request and get Response
NSData *returnData = [NSURLConnection sendSynchronousRequest: request returningResponse: nil error: nil];
// Log Response
NSString *response = [[NSString alloc] initWithBytes:[returnData bytes] length:[returnData length] encoding:NSUTF8StringEncoding];
NSLog(@"%@",response);

Swift

// Create your request string with parameter name as defined in PHP file
var myRequestString: String = "title=\(eventTitle.text!)&description=\(eventDescription.text!)&city=\(eventCity.text!)"
// Create Data from request
var myRequestData: NSData = NSData.dataWithBytes(myRequestString.UTF8String(), length: myRequestString.characters.count)
var request: NSMutableURLRequest = NSMutableURLRequest(URL: NSURL(string: "http://www.youardomain.com/phpfilename.php")!)
// set Request Type
request.HTTPMethod = "POST"
// Set content-type
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "content-type")
// Set Request Body
request.HTTPBody = myRequestData
// Now send a request and get Response
var returnData: NSData = NSURLConnection.sendSynchronousRequest(request, returningResponse: nil, error: nil)
// Log Response
var response: String = String(bytes: returnData.bytes(), length: returnData.characters.count, encoding: NSUTF8StringEncoding)
NSLog("%@", response)

这篇关于从iOS发布数据到PHP脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆