2013-12-17 50 views
1

我使用這段代碼我使用「十六進制字符串」,但數據發送值不會發布到無法使用NSMutableURLRequest

"http://localhost/adi/adnan.php" 

發佈的數據,這是我的代碼

 NSLog(@"offset: %i colors: RGB A %i %i %i %i",offset,red,green,blue,alpha); 
     NSString *hexString=[NSString stringWithFormat:@"%02X%02X%02X",red,green,blue]; 
     NSURL *url = [NSURL URLWithString:@"http://localhost/adi/adnan.php"]; 
     NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url]; 
     NSString *messageBody = hexString; 
     NSString *msgLength = [NSString stringWithFormat:@"%d", [messageBody length]]; 
     [theRequest setHTTPMethod:@"POST"]; 
     [theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"]; 
     [theRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; 
     [theRequest setHTTPBody:[messageBody dataUsingEncoding:NSUTF8StringEncoding]]; 
     NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self]; 
    if(theConnection) 
        { 
    NSLog(@"Connection Successful"); 
    NSURLResponse *response; 
    NSError *err; 
    NSData *responseData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&response error:&err]; 
    NSLog(@"responseData here is: %@", responseData); 
    NSString *str = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]; 
    NSLog(@"responseData: %@", str);   
    receivedata=[NSMutableData data]; 
    NSLog(@"data is: %@",receivedata); 
      } 

這是我的PHP代碼

<?php 
    echo "Engr.Adnan; 

// $abc= $_GET['name']; 

    $abc = $_POST['action']; 
    print_r($_REQUEST); 
    echo $adnan = strrev($abc); 

?> 

和我得到這個類型的輸出或響應

2013-12-17 01:33:55.615 VBColorPicker[25216:c07] offset: 27744 colors: RGB A 52 255 36 255 
2013-12-17 01:33:55.650 VBColorPicker[25216:c07] Connection Successful 
2013-12-17 01:33:55.674 VBColorPicker[25216:c07] responseData: <456e6772 2e41646e 616e4172 7261790a 280a290a> 
2013-12-17 01:33:55.675 VBColorPicker[25216:c07] responseData: Engr.AdnanArray 
(
) 
2013-12-17 01:33:55.675 VBColorPicker[25216:c07] data is: <> 

回答

1

你從表單中的數據丟失的參數名:

NSString *hexString = [NSString stringWithFormat:@"%02X%02X%02X", red, green, blue]; 

應該

NSString *hexString = [NSString stringWithFormat:@"color=%02X%02X%02X", red, green, blue]; 

,然後檢索它的服務器端這樣

$color = $_POST['color']; 

或者,如果您不想指定參數名稱,則可以獲取完整的re尋求身體服務器端這樣的:

$requestBody = file_get_contents('php://input'); // will be the hex string 
+1

感謝您的回答 –

相關問題