从iOS发布数据到PHP脚本

编程入门 行业动态 更新时间:2024-10-23 10:26:28
本文介绍了从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:@"mydomain/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/ios/form.php时,它会显示标题,说明和城市空白。

domain/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: @"www.youardomain/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: "www.youardomain/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脚本

本文发布于:2023-10-11 23:21:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1483125.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:脚本   数据   iOS   PHP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!