我有一个应用程序,使用NSURLRequest和NSURLConnexion将数据发送到PHP脚本。我想要的是用post方法发送像“Hello world”这样的数据,我希望在我的应用程序“my world from myPHPScript.php”中显示!但我没有Web服务经验,尤其是PHP。
这是我发送请求的objective-c代码:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL
URLWithString:@"http://localhost:8888/testNSURL/index.php"]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
[request setHTTPMethod:@"POST"];
NSString *postString = @"myVariable=Hello world !";
[request setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:request delegate:self];
if (theConnection) {
receiveData = [NSMutableData data];
}
这是我的PHP代码:
<?php
if(isset($_REQUEST["myVariable"])) {
echo json_encode("Hello from index.php !");
}
else echo json_encode('Request failed');
?>
我的问题是:
echo json_encode("Hello from index.php !");
?答案 0 :(得分:0)
我个人在将内容发布到服务器时使用ASIFormDataRequest。当您从此方法获取信息时,您可以执行类似这样的操作,以获取服务器上已回显的所有数据/字符串值:
- (void)requestFinished:(ASIHTTPRequest *)aRequest {
NSData *responseData = [aRequest responseData];
NSString *responseString = [aRequest responseString];
}
从数据信息中获取字符串的方式如下:
[[NSString alloc] initWithData:responseData encoding:NSASCIIStringEncoding]