开发者

Emoji to JSON encoded, post to web server

开发者 https://www.devze.com 2023-03-31 07:18 出处:网络
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

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


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.


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


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.
0

精彩评论

暂无评论...
验证码 换一张
取 消