从iOS发布简单的字符串到PHP脚本

时间:2015-07-24 12:27:18

标签: php ios objective-c

这似乎是以前曾多次被问过的问题,但是我找不到任何答案。我想从我的iOS应用程序向我的服务器发送一些纯文本字符串。在服务器端,我有一个准备好等待的PHP脚本,我的应用程序使用POST功能,但是我无法运行它。请帮助:)

服务器上的PHP代码(upload.php):

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <NSURLConnectionDelegate>{

    IBOutlet UITextField *header, *description, *city;
    IBOutlet UILabel *serverResponse;
}

@end

ViewController.h

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController{
    NSMutableData *mutableData;

#define URL            @"http://myserver/upload.php"  // change this URL
#define NO_CONNECTION  @"No Connection"
#define NO_VALUES      @"Please enter parameter values"


}

- (void)viewDidLoad {

    [super viewDidLoad];

}

-(IBAction)sendDataUsingPost:(id)sender{

    [self sendDataToServer :@"POST"];

}

-(void) sendDataToServer : (NSString *) method{

    NSString *head  = header.text;
    NSString *desc = description.text;
    NSString *cty  = city.text;

    if(head.length > 0 && desc.length > 0 && cty.length > 0){

        NSURL *url = nil;
        NSMutableURLRequest *request = nil;

        // Only Difference between POST and GET is only in the way they send parameters

        if([method isEqualToString:@"POST"]){

            NSString *parameter = [NSString stringWithFormat:@"header=%@&description=%@&city=%@",head, desc, cty ];
            NSData *parameterData = [parameter dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
            ;
            url = [NSURL URLWithString: @"http://myserver/upload.php"];
            request = [NSMutableURLRequest requestWithURL:url];
            [request setHTTPBody:parameterData];

        }

        [request setHTTPMethod:method];
        [request addValue: @"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:@"Content-Type"];

        NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];

        if( connection )
        {

            mutableData = [NSMutableData new];

        }else{

            serverResponse.text = NO_CONNECTION;

        }

    }else{

        serverResponse.text = NO_VALUES;

    }

}

#pragma mark NSURLConnection delegates

-(void) connection:(NSURLConnection *) connection didReceiveResponse:(NSURLResponse *)response
{
    [mutableData setLength:0];
}

-(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [mutableData appendData:data];
}

-(void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    serverResponse.text = NO_CONNECTION;
    return;
}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSString *responseStringWithEncoded = [[NSString alloc] initWithData: mutableData encoding:NSUTF8StringEncoding];
    //NSLog(@"Response from Server : %@", responseStringWithEncoded);
    NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[responseStringWithEncoded dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
    serverResponse.attributedText = attrStr;
}

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

@end

ViewController.m

{{1}}

1 个答案:

答案 0 :(得分:0)

在echo中使用$_POST代替$_REQUEST。您已经为变量分配了$_POST个值。因此,您可以使用echo $header;代替$_POST['header']

尝试

<?php
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
    $header = $_POST['header'];
    $description = $_POST['description'];
    $city = $_POST['city'];

    echo "Header, description and city:" , '<br>', '<br>';

    echo "Header : " .$header.'<br>';
    echo "Desc : ".$description.'<br>';
    echo "City : ".$city.'<br>';
}
else
{
    echo 'There is no Post Request';
}
?>

您可以var_dump($_POST);查看您在帖子中收到的内容。