title: PHP Unicode in JSON
body: <p>I'm sending a JSON POST body to my PHP web service that looks something like this:</p>
  
  <pre><code>{
      "foo": "☺"
  }
  </code></pre>
  
  <p>When I echo out the body in the PHP, I see this:</p>
  
  <pre><code>{
      "foo":"\xe2\x98\xba"
  }
  </code></pre>
  
  <p>I've also tried sending the <code>\uXXXX</code> equivalent:</p>
  
  <pre><code>{
      "foo": "\u263a"
  }
  </code></pre>
  
  <p>This got further, in that the raw JSON string received had <code>"foo":"\\u263a"</code>, but after <code>json_decode</code> the value turned to <code>\xe2\x98\xba</code>.</p>
  
  <p>This is causing problems when I come to use the value in a JSON response. I get:</p>
  
  <pre><code>json_encode(): Invalid UTF-8 sequence in argument
  </code></pre>
  
  <p>At its simplest, this is what happens why I try to JSON encode the string:</p>
  
  <pre><code>&gt; 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
  </code></pre>
  
  <p><strong>My question is</strong>: how can I best get this smiley face from one end of my application to the other?</p>
  
  <p>I'd appreciate any help you could offer.</p>