0

我正在尝试将一个变量从 iOS Xcode 发布到 PHP。下面是我的脚本,但 email 变量不会在我的 PHP 中发布:

php code $email_from = $_POST['email'];

思维email将来自iOS。

下面是Xcode:

    //Start of general request setup
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:serverLink]];
[request setHTTPMethod:httpMethod];
[request setTimeoutInterval:connectionTimeout];
NSString *stringBoundary = [NSString stringWithString:@"0xKhTmLbOuNdArY"];
NSString *contentType    = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", stringBoundary];
[request addValue:contentType forHTTPHeaderField:@"Content-Type"];
NSString *email =[NSString stringWithFormat:@"ryan"];
//End of general request setup


//Start of body setup
NSArray *parametersKeys = [parameters allKeys];
NSArray *imagesKeys = [images allKeys];

NSMutableData *postBody = [NSMutableData data];
[postBody appendData:[[NSString stringWithFormat:@"\r\n\r\n--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
BOOL addedData = NO;

for (NSString *parameterKey in parametersKeys) {
    if (addedData) {
        [postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
    }
    else {
        addedData = YES;
    }
    [postBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", parameterKey] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[[NSString stringWithFormat:@"%@", [parameters objectForKey:parameterKey]] dataUsingEncoding:NSUTF8StringEncoding]];
}

for (NSString *imageKey in imagesKeys) {
    if (addedData) {
        [postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
    }
    else {
        addedData = YES;
    }
    [postBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"%@.jpg\"\r\n", imageKey, imageKey] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[@"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; email=\"%@\"\r\n""", email] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[images objectForKey:imageKey]];
}    

[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:postBody];
//End of body setup

NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

[parameters release];
[images release];

isSending = NO;
4

1 回答 1

0

添加这个有效

    [postBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"%@.jpg\"\r\n", imageKey, imageKey] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[@"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[images objectForKey:imageKey]];

    [postBody appendData:[[NSString stringWithFormat:@"\r\n\r\n--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"email\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[[NSString stringWithFormat:@"email@email.com"] dataUsingEncoding:NSUTF8StringEncoding]];  // Set Email to
于 2013-01-09T01:16:12.910 回答