Emoji 转 JSON 编码,发布到 Web 服务器

发布于 2024-12-01 05:11:18 字数 2068 浏览 0 评论 0原文

请帮助我解决发布 JSON 解码表情符号字符时遇到的问题。 我有一个 UITextView,这个文本视图可能有表情符号字符。我将数据发布到 Web 服务器,UITextView.text 以 JSON 形式呈现,问题是当文本有表情符号时,我无法获取数据。我所做的是:

$postData = file_get_contents("php://input") to get the data.

然后我使用

$post = json_decode($postData,true);

解码数据并拥有一个关联数组并将数据插入数据库中。

这是我将数据插入数据库时​​的代码片段。

$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 中的数据(解码数据)为空。

我尝试使用虚拟数据(代码片段中的第 2 行)

//$postData = '{"body":"characters here ","subject":"subject here","username":"janus","from_id":"185","to_id":"62"}';

,并成功将表情符号字符插入数据库中。我不知道为什么,但当数据来自设备时它的工作方式不同 ($postData = file_get_contents("php://input"))

这就是我在客户端中编码和发布数据的方式。

    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];

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.

then I use

$post = json_decode($postData,true);

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);

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.

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"}';

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];

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

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

发布评论

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

评论(3

〃温暖了心ぐ 2024-12-08 05:11:19

一旦数据发送到您的 php 脚本,您需要将其转换为多字节字符串:

$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 类型,并且该函数正确转换了它。

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');

You can use this function:

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;
}

Edit: The data was probably of type application/x-www-form-urlencoded for us and that function converted it correctly.

孤檠 2024-12-08 05:11:19

表情符号字符很可能以 UNICODE 进行转码,因此仅以 UTF-8 格式发送、接收和管理数据就足够了。

当接收这个

$postData = file_get_contents("php://input") 

(我认为这是一个真实的 URL)时,请确保您的 php 脚本发送一个 Content-Encoding 标头(如下所示,选择适合您的 MIME 类型)

 header("Content-Type: text/html; charset=utf-8");

emoji characters are most likely transcoded in UNICODE, so it should be sufficient to just send, receive and manage your data in UTF-8.

When receiving with this

$postData = file_get_contents("php://input") 

(I suppose that is a real URL), make sure your php script sends an Content-Encoding header (like the following, choose a MIME-type that suits you)

 header("Content-Type: text/html; charset=utf-8");
北方。的韩爷 2024-12-08 05:11:19

请按照以下步骤操作:

  • 将 Emoji 字符转换为 Base64 并发送到服务器。
  • 在服务器端将 Base64 保存在数据库中而不进行解码。
  • 当您想在应用程序上显示表情符号时,请从服务器检索相同的 Base64 数据。
  • 解码检索字符串并显示在应用程序上。
  • 您的表情符号角色将正确显示。

Please follow following steps:

  • Convert Emoji characters to base64 and send to server.
  • On server side save base64 in database without decode.
  • When you want to display Emoji on Application then retrieve same base64 data from server.
  • Decode retrieve string and display on app.
  • Your Emoji character will display properly.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文