send Post request to PHP Server

2019-06-12 16:08发布

I tried so send some data from two textfields with my button, once i press the button I receive an E-mail without the content of the textfields which I typed in my iOS Simulator. I can not find the problem, please help me.

ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
{
    UITextField * firstname;
    UITextField *lastname;
    UIButton *sendPost;
}
@property (strong, nonatomic) IBOutlet UITextField *firstname;
@property (strong, nonatomic) IBOutlet UITextField *lastname;
@property (strong, nonatomic) IBOutlet UIButton *postButtonTapped;


-(IBAction)postButtonTapped:(id)sender;


@end

ViewController.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
@synthesize firstname;
@synthesize lastname;

- (void)viewDidLoad
{
    [super viewDidLoad];
}

- (IBAction)postButtonTapped:(id)sender
{
    NSString *myRequestString = [NSString stringWithFormat:@"firstname=%@&lastname=%@",firstname.text,lastname.text];
    NSData *myRequestData = [NSData dataWithBytes: [myRequestString UTF8String] length: [myRequestString length]];

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString: @"http://textfake.com/test/mail.php"]];

    [request setHTTPMethod: @"POST"];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
    [request setHTTPBody: myRequestData];

    NSData *returnData = [NSURLConnection sendSynchronousRequest: request returningResponse: nil error: nil];
    NSString *response = [[NSString alloc] initWithBytes:[returnData bytes] length:[returnData length] encoding:NSUTF8StringEncoding];

    NSLog(@"%@",response);
}

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.firstname resignFirstResponder];
    [self.lastname resignFirstResponder];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

PHP Code

<?php
header("Content-type: text/html; charset=UTF-8");
$firstname = $_POST["firstname"];
$lastname = $_POST["lastname"];
$text = "not set";
if(!empty($_POST["text"])){
    $text = $_POST["text"];
}
$lat = $_POST["lat"];
$lon = $_POST["lon"];
$dieMessage =$firstname." ".$lastname."\n".$text."\n";
$dieMessage .="Lat:".$lat."\n";
$dieMessage .="Lon:".$lon;
mail("fortesing@gmail.com", "gps daten", $dieMessage);
//echo 'Email abgeschickt!';
echo $dieMessage;
?>

3条回答
Explosion°爆炸
2楼-- · 2019-06-12 16:23

AFNetworking 2.0

- (IBAction)postButtonTapped:(id)sender
{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    NSDictionary *params = @{@"firstname": firstname.text,
                             @"lastname": lastname.text};
    [manager POST:@"http://textfake.com/test/mail.php" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"JSON: %@", responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
    }];
}

Hope this works.

查看更多
Explosion°爆炸
3楼-- · 2019-06-12 16:26

I solved the problem, seems like it had some problems with:

[NSURL URLWithString: @"http://textfake.com/test/mail.php"]];

and it should be with www. instead:

[NSURL URLWithString: @"http://www.textfake.com/test/mail.php"]];
查看更多
我命由我不由天
4楼-- · 2019-06-12 16:34

for the iOS part, try this :

- (IBAction)postButtonTapped:(id)sender {
    NSURL *url = [NSURL URLWithString:@"http://textfake.com/test/mail.php"];
    NSString *post = [NSString stringWithFormat:@"firstname=%@&lastname=%@",firstname.text,lastname.text];

    NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];
    // set POST method
    [urlRequest setHTTPMethod:@"POST"];
    // adding keys with values
    NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
    NSString *postLength = [NSString stringWithFormat:@"%d",[postData length]];
    [urlRequest addValue:postLength forHTTPHeaderField:@"Content-Length"];
    [urlRequest setHTTPBody:postData];

    NSURLSession *urlSession = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration ephemeralSessionConfiguration]];

    NSURLSessionDownloadTask *urlSessionDownloadTask = [urlSession downloadTaskWithRequest:urlRequest completionHandler:^(NSURL *localfile, NSURLResponse *response, NSError *error) {

        NSData *data = [NSData dataWithContentsOfURL:localfile];
        //do what you want with your data here. PS your are not on main thread !
    }];
    [urlSessionDownloadTask resume];
}

For the PHP part, you could not get text, lat, lon as you never post it !

查看更多
登录 后发表回答