title: PHP json_decode variables not being set
body: <p>UPDATE:</p>
  
  <p>Ill be honest, I gave up and did it in sails.js in the end, I will leave this question up in case anyone can see where I went wrong.</p>
  
  <hr>
  
  <p>wondering if anyone can help me out with a bit of a strange one I can't seem to solve.</p>
  
  <p>I have an oAuth script that returns an output of a json array back to my android app.</p>
  
  <p>The issue I am having is undefined index on all the json variables, however if I uncomment the two lines commented in my code and take a look on the browser then the $hdr line looks exactly how it should, its just when I try to return the $hdr in my header I receive:</p>
  
  <pre><code>callback://com.herghost.waves?code=&amp;account_name=&amp;refresh_token=&amp;expires_in=
  </code></pre>
  
  <p>Here is the script I am using:</p>
  
  <pre><code>error_reporting(E_ALL);
  $code = $_GET['code'];
  $clientId = "";
  $clientSecret = "";
  $redirectionUrl = rawurlencode("http://waves-app.com/generate_token.php");
  $url = sprintf("https://cloud.digitalocean.com/v1/oauth/token?client_id=%s&amp;client_secret=%s&amp;grant_type=authorization_code&amp;code=%s&amp;redirect_uri=%s", $clientId, $clientSecret, $code, $redirectionUrl);
  
  $fields = array();
  $fields_string = "";
  $ch = curl_init();
  curl_setopt($ch,CURLOPT_URL, $url);
  curl_setopt($ch,CURLOPT_POST, count($fields));
  curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
  curl_setopt($ch,CURLOPT_RETURNTRANSFER, TRUE);
  
  $result = curl_exec($ch);
  curl_close($ch);
  
  $json = json_decode($result, TRUE);
  //print_r($json);
  if (empty($code)) $code = '';
  if (empty($name)) $name = '';
  if (empty($refresh)) $refresh = '';
  if (empty($expires)) $expires = '';
  
  $code = $json['access_token'];
  $name = rawurlencode($json["info"]["name"]);
  $refresh = $json["refresh_token"];
  $expires =  $json["expires_in"];
  $hdr = sprintf("Location:callback://com.herghost.waves?code=%s&amp;account_name=%s&amp;refresh_token=%s&amp;expires_in=%s", $code, $name, $refresh,$expires);
  //echo $hdr;
  header($hdr);
  </code></pre>
  
  <p>EDIT: Other things I have now tried</p>
  
  <p>1) Setting a hardcoded string in the sprintf</p>
  
  <pre><code>Location:callback://com.herghost.waves?code=1234&amp;account_name=%s&amp;refresh_token=%s&amp;expires_in=%s"
  </code></pre>
  
  <p>I get the correct code back in my android app</p>
  
  <p>2) Setting a variable manually and passing this</p>
  
  <pre><code>$test = 'test'
  header(sprintf("Location:callback://com.herghost.waves?code=%s&amp;account_name=%s&amp;refresh_token=%s&amp;expires_in=%s",
              $test, rawurlencode($json["info"]["name"]), $json["refresh_token"]//, $json["expires_in"]));
  </code></pre>
  
  <p>I get test come back in the android app!</p>
  
  <p>I have also checked to see if there are any curl errors by using</p>
  
  <pre><code>if(curl_errno($ch))
  {
      echo 'Curl error: ' . curl_error($ch);
  }
  </code></pre>
  
  <p>No error.</p>
  
  <p>var_dump is definitely outputting the results so why are my variables empty?!</p>
  
  <p>ANOTHER EDIT</p>
  
  <p>So I have tried this:</p>
  
  <pre><code>$test = '45678';
  $test2 = $json["refresh_token"];
  $jsonerr = json_last_error();
  
  
  echo $test2;
  if($jsonerr == 0)
  {
  header("Location:callback://com.herghost.waves?code=" . $test2 . "+" . $test . "&amp;account_name=1&amp;refresh_token=2&amp;expires_in=3");
  }
  else
  {
      header("Location:callback://com.herghost.waves?code=error&amp;account_name=1&amp;refresh_token=2&amp;expires_in=3" . $jsonerr);
  }
  </code></pre>
  
  <p>and my response is:</p>
  
  <pre><code>callback://com.herghost.waves?code=+45678&amp;account_name=1&amp;refresh_token=2&amp;expires_in=3
  </code></pre>
  
  <p>Never felt so infuriated over a simple piece of code!</p>