I'm sending a JSON POST body to my PHP web service that looks something like this:
{
"foo": "☺"
}
When I echo out the body in the PHP, I see this:
{
"foo":"\xe2\x98\xba"
}
I've also tried sending the \uXXXX equivalent:
{
"foo": "\u263a"
}
This got further, in that the raw JSON string received had "foo":"\\u263a", but after json_decode the value turned to \xe2\x98\xba.
This is causing problems when I come to use the value in a JSON response. I get:
json_encode(): Invalid UTF-8 sequence in argument
At its simplest, this is what happens why I try to JSON encode the string:
> php -r 'echo json_encode("\x98\xba\xe2");'
PHP Warning: json_encode(): Invalid UTF-8 sequence in argument in Command line code on line 1
My question is: how can I best get this smiley face from one end of my application to the other?
I'd appreciate any help you could offer.
echo "\xe2\x98\xba";works; shows the correct unicode smilie.error_log(file_get_contents('php://input')).