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 に答える