将 UTF-8 文本传递给 php 脚本

发布于 2024-11-09 12:28:05 字数 1671 浏览 0 评论 0原文

你好, 我允许人们在我的应用程序中输入包含非拉丁字符的文本,例如西里尔字母等。尽管如此,无论我做什么,都会出现问号。 这是我在 Objective-C 程序上的代码:

    NSMutableURLRequest *theRequest=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/iPhone/php/insert.php?recipientEmail=%@&passwd=%@&email=%@&personalPasswd=%@&private=%d&latitude=%f&longitude=%f&altitude=%f&horizontal=%f&vertical=%f&disclosedEmail=%d", appDelegate.actualHttpHeader, myRecipient, myKey, email, passwd, myPrivate?1:0, myLatitude, myLongitude, myAltitude, myHeading, myTilting, mySigning?1:0]]
                                                            cachePolicy:NSURLRequestUseProtocolCachePolicy                                                          timeoutInterval:60.0];
    //NSLog(@"message is:%@",myMessage);
    //myMessage = [myMessage stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSLog(@"in graffitiSubm message=%@", myMessage);
    [theRequest addValue:myMessage forHTTPHeaderField:@"message"];
    [theRequest addValue:street forHTTPHeaderField:@"street"];
    [theRequest addValue:city forHTTPHeaderField:@"city"];
    [theRequest addValue:country forHTTPHeaderField:@"country"];
    [theRequest setValue: @"text/xml;charset=UTF-8" forHTTPHeaderField:@"Content-Type"];
    NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

我尝试在 URL 请求中使用 UTF-8 设置(如您所见),以及 NSLog 下现在评论的部分。 NSLog 报告了我进行测试时使用的正确西里尔字符。

当我在 php 脚本 (insert.php) 开头打印 Message 字符串的值时,它仍然带有问号。 如果我在 php 中手动编写西里尔测试,它会被正确处理,甚至会像这样插入到数据库中。

我应该在协议中更改什么以使 UTF-8 字符正确通过?

谢谢,法布里齐奥

Hullo,
I would allow people to enter in my application also text with non latin characters, like cyrillic and the kind. Still, whatever I do question marks appear instead.
This is the code I have on my objective-c program:

    NSMutableURLRequest *theRequest=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/iPhone/php/insert.php?recipientEmail=%@&passwd=%@&email=%@&personalPasswd=%@&private=%d&latitude=%f&longitude=%f&altitude=%f&horizontal=%f&vertical=%f&disclosedEmail=%d", appDelegate.actualHttpHeader, myRecipient, myKey, email, passwd, myPrivate?1:0, myLatitude, myLongitude, myAltitude, myHeading, myTilting, mySigning?1:0]]
                                                            cachePolicy:NSURLRequestUseProtocolCachePolicy                                                          timeoutInterval:60.0];
    //NSLog(@"message is:%@",myMessage);
    //myMessage = [myMessage stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSLog(@"in graffitiSubm message=%@", myMessage);
    [theRequest addValue:myMessage forHTTPHeaderField:@"message"];
    [theRequest addValue:street forHTTPHeaderField:@"street"];
    [theRequest addValue:city forHTTPHeaderField:@"city"];
    [theRequest addValue:country forHTTPHeaderField:@"country"];
    [theRequest setValue: @"text/xml;charset=UTF-8" forHTTPHeaderField:@"Content-Type"];
    NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

I tried both using the UTF-8 setting in the URL request, as you may see, and the now commented piece under the NSLog.
The NSLog reports the correct cyrillic characters I made tests with.

Still when I print the value of the Message string at the beginning of the php script (insert.php) it has question marks instead.
If I manually write the cyrillic test in the php, it is correctly handled and even inserted in the DB as such.

What should I change in the protocol to have UTF-8 characters pass through correctly?

Thanks, Fabrizio

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

终止放荡 2024-11-16 12:28:05

这里有许多组件在起作用:

  1. 文本输入
  2. 应用程序内部处理
  3. 通过 HTTP
  4. 输出到浏览器的
  5. 传输浏览器显示字符

如果文本在输入时以 UTF-8 进行编码,并且步骤 2 - 4 都不会改变无论以何种方式处理原始字符串,您都应该始终处理 Unicode。问题是您的浏览器可能不知道这一点,并试图将文本解释为其他内容。通过设置适当的标头告诉浏览器它正在处理 UTF-8 文本:

header('Content-Type: text/html; charset=UTF-8');

There are many components at work here:

  1. text input
  2. app internal handling
  3. transport over HTTP
  4. output to browser
  5. browser displaying characters

If the text is encoded in UTF-8 when it's input and none of steps 2 - 4 alter the raw string in any way, you should always be dealing with Unicode. The problem is likely that your browser does not know this and is trying to interpret the text as something else. Tell the browser it's dealing with UTF-8 text by setting an appropriate header:

header('Content-Type: text/html; charset=UTF-8');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文