我需要从iPhone发送一些参数到服务器中的php
PHP:
$text = $_GET['text']);
$mail = $_GET['mail']);
$phone = $_GET['phone']);
iPhone SDK
NSString *text = [[NSString alloc] initWithString:@""]; //very long text
NSString *mail = [[NSString alloc] initWithString:@"any@any.com"];
NSString *phone = [[NSString alloc] initWithString:@"456233876"];
如何将此字符串发送给PHP?
答案 0 :(得分:7)
以下是使用NSURLConnection创建HTTP POST请求以发布字段“text”,“mail”和“phone”的方法:
NSURLRequest *req = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://www.myserver.com/page.php"]];
NSMutableString *postBody = [NSMutableString stringWithFormat:@"text=%@", [@"very long text" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
[postBody appendFormat:@"&mail=%@", [@"any@any.com" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
[postBody appendFormat:@"&phone=%@", [@"1231231234" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
[req setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
[req setHTTPBody:[body dataUsingEncoding:NSUTF8StringEncoding]];
[req setHTTPMethod:@"POST"];
NSURLConnection *conn = [NSURLConnection connectionWithRequest:req delegate:self];
// Now, implement NSURLConnection delegate methods such as connectionDidFinishLoading: and connection:didFailWithError: to do something with the response from the server.
答案 1 :(得分:2)
正常使用GET或POST数据。这很简单。然后使用NSURLConnection发送数据,并将方法更改为POST。或者您可以使用GET,只需发送http://www.mysite.com/mypage.php?var1=abc&var2=def等网址