我在向服务器上的 PHP 文件发送信息时遇到问题。 我需要一些帮助,我放弃了,几乎可以放弃前面的这个问题。
IBAction:-
- (IBAction)reg:(id)sender {
NSInteger success = 0;
@try {
NSString *rawStr = [NSString stringWithFormat:@"username=%@",self.txtEmail.text];
NSData *data = [rawStr dataUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:@"http://something.com/register.php"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:data];
NSURLResponse *response;
NSError *err;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
NSLog(@"responseData: %@", responseData);
}
@catch (NSException * e) {
NSLog(@"Exception: %@", e);
[self alertStatus:@"Sign in Failed." :@"Error!" :0];
}
if (success) {
[self performSegueWithIdentifier:@"login_success" sender:self];
}
}
这会发送到我服务器中的 PHP 文件
<?php
$email = $_GET['username'];
$email2 = $_POST['username'];
mail('thepridelagoon@gmail.com','testing' , $email,$email2);
?>
我尝试测试这两个函数以发送到我的电子邮件,但由于某种原因电子邮件是空的。 我做错了吗?? 请帮帮我。
最佳答案
正确答案是:
NSString *rawStr = [NSString stringWithFormat:@"username=%@",self.txtEmail.text];
NSData *data = [rawStr dataUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:@"http://something.com/register.php"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:data];
NSURLResponse *response;
NSError *err;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
NSLog(@"responseData: %@", responseData);
关于php - iOS 向 PHP 发送数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23872382/