通过POST从iOS发送UTF 8到PHP

时间:2012-03-24 01:04:40

标签: php ios utf-8 base64 nsurlrequest

所以我一直在研究如何让我的UTF8正确打印。我在这里找到了一个很好的例子: UTF8 Encoding problem - With good examples

所以我自己设置测试,但结果如下:(PHP)

echo "<meta http-equiv="Content-Type" content="text/html;charset=utf-8">";
mb_internal_encoding("UTF-8");
mb_http_output("UTF-8");
mb_http_input("UTF-8");

$textEnc = _POST["text"];
$text = cc_decode($textEnc);
echo 'Original : ', $text."<br />";
$enc = mb_detect_encoding($text, "UTF-8,ISO-8859-1");

echo 'Detected encoding '.$enc."<br />";

echo 'Fixed result: '.iconv($enc, "UTF-8", $text)."<br />";

结果:(结果HTML)

Original : ひ
Detected encoding UTF-8
Fixed result: ひ

我期待一个日本人物ひ

我用来创建请求的代码如下:(Obj C)

NSString* cCode = B64EncodeString([NSString stringWithFormat:@"%s",data->Text()]);
NSString* connectURL = [NSString stringWithFormat:@"%@%@", SERVER_ADDRESS, @"users/test.php"];

NSString* mPostArgs = [NSString stringWithFormat:@"text=%@", cCode];

NSMutableURLRequest* theRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]
                                         cachePolicy:NSURLRequestUseProtocolCachePolicy
                                         timeoutInterval:mTimeoutInSeconds];
[theRequest setValue:@"application/x-www-form-urlencoded; charset=UTF-8" forHTTPHeaderField:@"content-type"];
[theRequest setValue:@"UTF-8" forHTTPHeaderField:@"accept-charset"];

if(mPostArgs != nil) {

    NSData* myRequestData = [NSData dataWithBytes: [mPostArgs UTF8String] length: [mPostArgs length]];
    [theRequest setHTTPMethod: @"POST"];
    [theRequest setHTTPBody: myRequestData];
}

// create the connection with the request
// and start loading the data
NSURLConnection* theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

1 个答案:

答案 0 :(得分:0)

NSString* cCode = B64EncodeString([NSString stringWithFormat:@"%s",data->Text()]);

错误地取了字符串,我把它更改为不重新创建NSString

NSString* cCode = B64EncodeRaw((const uint8_t*)data->Text(), strlen(data->Text()));