title: PHP - Weird behaviour with JSON decoded variable
body: <p>I have a rather strange behavior in my app and run out of ideas. I'm trying to create a small REST API using Slim. I use cURL to call the page and the requests are in JSON format. Slim reads the body and decodes using <code>json_decode(value, true)</code> to return an Array. Please find the post function:</p>
  
  <pre><code>$app-&gt;post('/users', function () use ($app, $log)
  {
     $body = $app-&gt;request()-&gt;getBody();
     echo $body['username']; // Will throw Illegal string offset 'username'
  
     if (isset($body["username"]) &amp;&amp; isset($body["password"]))
     {
        echo $body['username']; // Will work
     }
     else
     {
        throw new \API\Exception\ValidationException("Invalid arguments"); // Is thrown all the time
     }
  });
  </code></pre>
  
  <p><strong>Problem 1</strong>:
  Even though <code>$body</code> is returned as array from <code>json_decode</code>, accessing a member will throw an <code>Illegal string offset</code> error and stop the code. </p>
  
  <pre><code> var_dump($body); // array(2) { ["username"]=&gt; string(8) "testuser"    ["password"]=&gt; string(8) "testpass" }
   echo is_array($body); // 1
   echo $body['username']; // Illegal string offset 'username'
   echo array_key_exists('username', $body); // Illegal string offset 'username'
  </code></pre>
  
  <p>The solution I found is placing <code>isset($body[key])</code> and the code will go on like nothing happens. Let's say this is a fix, but one I do not like. I would like to understand why this happens. However it still leads me to problem 2.</p>
  
  <p><strong>Problem 2</strong>:
  If I try to place an exception outside the <code>isset</code> block, it will throw it every time (even if the parameter has a value or not).</p>
  
  <p>This makes no sense to me and I tried everything that comes to mind. I have also a <code>GET</code> <code>/users/:username</code> where I also validate the username using an Exception and all works just fine. It only happens after reading parameters from a <code>POST</code> encoded in JSON. </p>
  
  <p>I have tested both under IIS(servers are on Windows) and Apache, and also PHP 5.5 and 5.6 (both x64).</p>
  
  <p>What I am doing wrong? Thank you in advance.</p>
  
  <p>EDIT:</p>
  
  <p>Also the cURL call:</p>
  
  <pre><code>$ch = curl_init();
  curl_setopt($ch, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_1);
  curl_setopt($ch, CURLOPT_CAINFO, "pemPath"); //Intentionally deleted the CAinfo path
  curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_NTLM);
  curl_setopt($ch, CURLOPT_USERPWD, "myauth"); //Intentionally deleted credentials
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  curl_setopt($ch, CURLOPT_URL, "https://localhost/api/v1/users");
  curl_setopt($ch, CURLOPT_HTTPHEADER, array("X-HTTP-Method-Override: POST", "Content-Type: application/json; charset=UTF-8"));
  $postArr = array("username" =&gt; "testuser", "password" =&gt; "testpass");
  curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params));
  $result = curl_exec($ch);
  </code></pre>