【问题标题】:Sending UTF 8 from iOS to PHP via POST通过 POST 将 UTF 8 从 iOS 发送到 PHP
【发布时间】:2012-03-24 01:04:39
【问题描述】:

所以我一直在研究如何让我的 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];

【问题讨论】:

  • 你真的不需要做那么多。一个简单的bin2hex($_POST['text']) 能得到什么?
  • 我不知道bin2hex,谢谢,很有帮助

标签: php ios utf-8 base64 nsurlrequest


【解决方案1】:
NSString* cCode = B64EncodeString([NSString stringWithFormat:@"%s",data->Text()]);

错误地获取了字符串,我将其更改为不重新创建 NSString

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

【讨论】:

    猜你喜欢
    • 2013-02-05
    • 1970-01-01
    • 2017-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多