问题描述
请帮我解决发布JSON解码的表情符号问题。
我有一个UITextView,这个文本视图可能有一个表情符号。我将数据发布到一个Web服务器,其中UITextView.text呈现为JSON,问题是当文本有一个表情符号时,我无法获取数据。我做的是:
Please help me with my problem in posting a JSON decoded emoji character.I have a UITextView, this text view may have a emoji character. I am posting the data to a web server with the UITextView.text presented as JSON, the problem is when the text has a an emoji, I am not able to get the data. What I do is:
$postData = file_get_contents("php://input") to get the data.
然后我使用
$post = json_decode($postData,true);
解码数据,并具有assoc数组,并将数据插入数据库。
to decode the data and have a assoc array and insert the data in database.
当我将数据插入数据库时,这里是一个代码片段。
here is a code snippet when I insert my data into database.
$postData = file_get_contents("php://input");
//$postData = '{"body":"characters here ","subject":"subject here","username":"janus","from_id":"185","to_id":"62"}';
$post = json_decode($postData,true);
$data=array(
'user_id_from'=>mysql_real_escape_string($post['from_id']),
'user_id_to'=>mysql_real_escape_string($post['to_id']),
'subject'=>mysql_real_escape_string($post['subject']),
'message'=>mysql_real_escape_string($post['body']));
$messages_obj->insert($data);
没有找到表情符号,它工作正常。没问题。问题是当发现表情符号时,$ post(解码数据)中的数据为空。
Without an emoji character found, it works fine. no problem. the problem is when an emoji character found, the data in $post (decoded data) is null.
我试图使用虚拟数据(代码片段中的第2行) / p>
I tried to use dummy data (line 2 in code snippet)
//$postData = '{"body":"characters here ","subject":"subject here","username":"janus","from_id":"185","to_id":"62"}';
我成功地将表情符号插入到数据库中。我不知道为什么,但是当数据来自设备时,它不工作($ postData = file_get_contents(php:// input))
and I succesfully inserted the emoji characters in database. I dont know why but It dont work the same when the data is from the device ($postData = file_get_contents("php://input"))
这是怎么回事我编码并在客户端发布我的数据。
This is how I encode and post my data in client.
NSMutableDictionary *messageDetails = [[NSMutableDictionary alloc] init];
[messageDetails setObject:[loginItems objectForKey:@"user_id"] forKey:@"from_id"];
[messageDetails setObject:recipientID forKey:@"to_id"];
[messageDetails setObject:@"subject here" forKey:@"subject"];
[messageDetails setObject:newMessageField.text forKey:@"body"];
[messageDetails setObject:[loginItems objectForKey:@"username"] forKey:@"username"];
NSString *strPostData = [messageDetails JSONRepresentation];
[messageDetails release];
NSData *postData = [NSData dataWithBytes:[strPostData UTF8String] length:[strPostData length]];
[urlRequest setHTTPMethod:@"POST"];
[urlRequest setHTTPBody:postData];
BTW谁制作了这些表情符号?他破坏了我的生命!
BTW, who made these emoji characters? He is ruining my life!
推荐答案
将数据发送到您的php脚本后,需要将其转换为多字节字符串:
Once the data is sent to your php script you need to convert it to a multibyte string:
$content = mb_convert_encoding($content, 'UTF-8');
您可以使用此功能:
function cb($content){
if(!mb_check_encoding($content, 'UTF-8')
OR !($content === mb_convert_encoding(mb_convert_encoding($content, 'UTF-32', 'UTF-8' ), 'UTF-8', 'UTF-32'))) {
$content = mb_convert_encoding($content, 'UTF-8');
}
return $content;
}
编辑:数据可能是application / x-www-form-为我们提供了urlencoded,该功能正确转换。
The data was probably of type application/x-www-form-urlencoded for us and that function converted it correctly.
这篇关于表情符号到JSON编码,发布到Web服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!