php - 无法使用 NSMutableURLRequest 发布数据

标签 php ios iphone nsurlconnection 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: <>

最佳答案

您缺少表单数据中的参数名称:

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

应该是

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

然后像这样在服务器端检索它

$color = $_POST['color'];

或者,如果您不想指定参数名称,您可以像这样在服务器端获取完整的请求主体:

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

关于php - 无法使用 NSMutableURLRequest 发布数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20633338/

相关文章:

php - 未捕获的 OAuthException : An active access token must be used to query information about the current user

php - 将 3 个 MYSQL 查询优化为一个

ios - UICollectionViewFlowLayout 的行和列

iphone - 仅自动旋转 UITabBar 中的某些选项卡? (iOS 5)

iphone - NSString 中的原始字节使用什么编码

iphone - 使用 AFJSONRequestOperation 返回 JSON

php - 从数据库加载 jreviews 字段

ios - 尝试设置 NSLayoutConstraint 时出错

ios - 创建 iOS 应用程序的建议,该应用程序获取 JSON 响应并将数据存储在核心数据中(供离线使用)

php - 使用点表示法访问多维数组