title: php json_decode hang, unable to decode string from POST
body: <p>I'm new to PHP,JSON,js.  </p>
  
  <p>I have a js program that will send JSON string to PHP
  I have some problem when using the json_decode fn inside PHP.<br>
  I try to save the string receive from JS into a file, and the json string is correct without any problem.
  But when i try to use json_decode, the function hang (i think, since anything below the function call are not called at all, all my echo/print did not give anything.  </p>
  
  <p>Below is my js code:  </p>
  
  <pre><code>function post()
  {
    var test1 = {
      name:"marzzuq",
      age:16
    };
    myjson = JSON.stringify(test1);
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.open("POST","post.php",true);
    xmlhttp.setRequestHeader("Content-type","application/json");
    xmlhttp.send(myjson);
    console.log("\nsend ok: " + myjson);
  }
  </code></pre>
  
  <p>and below is my php script:  </p>
  
  <pre><code>&lt;?php
   $rawdata = file_get_contents('php://input');
   file_put_contents('/tmp/test.json', $rawdata);
   $jsson = json_decode($rawdata);
   file_put_contents('/tmp/test3.json', $jsson);
   `echo test &gt;&gt; /tmp/test.txt`;
   ?&gt;
  </code></pre>
  
  <p>I have tried to use htmlentities and html_entity_decode:<br>
  <code>$result=  htmlentities((string)$rawdata);<br>
  $result2 = html_entity_decode((string)$rawdata);</code>  </p>
  
  <p>and use the result on <code>json_decode</code> but it does not work.<br>
  the length fr <code>$rawdata</code> is 27 (using strlen), which is correct.</p>
  
  <p>I try to print the json_last_error, but like i say, all the code below the line <code>json_decode</code> stop working, so nothing will get print.  </p>
  
  <pre><code>switch (json_last_error()) {
      case JSON_ERROR_NONE:
          `echo ' - No errors' &gt;&gt; /tmp/test.txt`;
      break;
      case JSON_ERROR_DEPTH:
          `echo ' - Maximum stack depth exceeded' &gt;&gt; /tmp/test.txt`;
      break;
      case JSON_ERROR_STATE_MISMATCH:
          `echo ' - Underflow or the modes mismatch' &gt;&gt; /tmp/test.txt`;
      break;
      case JSON_ERROR_CTRL_CHAR:
          `echo ' - Unexpected control character found' &gt;&gt; /tmp/test.txt`;
      break;
      case JSON_ERROR_SYNTAX:
          `echo ' - Syntax error, malformed JSON' &gt;&gt; /tmp/test.txt`;
      break;
      case JSON_ERROR_UTF8:
          `echo ' - Malformed UTF-8 characters, possibly incorrectly encoded' &gt;&gt; /tmp/test.txt`;
      break;
      default:
          `echo ' - Unknown error' &gt;&gt; /tmp/test.txt`;
      break;
  }
  </code></pre>
  
  <p>Can anyone help me?</p>