将 UTF-8 文本传递给 php 脚本
你好, 我允许人们在我的应用程序中输入包含非拉丁字符的文本,例如西里尔字母等。尽管如此,无论我做什么,都会出现问号。 这是我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这里有许多组件在起作用:
如果文本在输入时以 UTF-8 进行编码,并且步骤 2 - 4 都不会改变无论以何种方式处理原始字符串,您都应该始终处理 Unicode。问题是您的浏览器可能不知道这一点,并试图将文本解释为其他内容。通过设置适当的标头告诉浏览器它正在处理 UTF-8 文本:
There are many components at work here:
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: