title: POST request to PHP REST API works only on Postman
body: <p>I'm trying to create and access my first Rest API and I'm getting trouble with the <strong>POST</strong> requests. If I test my API on <strong>Postman</strong>, it's working just fine. I can read, delete, etc.. </p>
  
  <p>But now I wanna send the data of a <strong>html form</strong> using PHP to my API to create a new record on the database. I'm using only <strong>PHP</strong> and <strong>Mysql</strong> for the project. And I tried to use <strong>CURL</strong> and <strong>file_gets_content</strong>, no success. </p>
  
  <p>What happens is that the array that the API receives is <strong>always empty</strong>. I'm debugging   every step right before the call and the array is not empty, so I imagine that there is something wrong with the call itself.</p>
  
  <p>Here is the part of my <strong>create.php</strong> that receives the data on my API:</p>
  
  <pre><code>&lt;?php  
  //configuracao de headers
  // Headers
  header('Access-Control-Allow-Origin: *');
  header("Content-Type: application/json; charset=UTF-8");
  header("Access-Control-Allow-Methods: POST");
  header("Access-Control-Max-Age: 3600");
  header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
  
  include_once('../../config/Banco.php');
  include_once('../../models/Curso.php');
  
  $banco = new Banco();
  $conexao = $banco-&gt;conecta();
  
  $curso = new Curso($conexao);
  
  //pega o q foi submetido
  $data = json_decode(file_get_contents("php://input"));
  
  if (empty($data-&gt;id_sala)){
      echo json_encode(array('mensagem'=&gt;'Preencha o id_sala'));
      die();
  }
  </code></pre>
  
  <p>And this is the part of a function that I use to call the create method of my API:</p>
  
  <pre><code>function criar_curso($nome, $id_professor, $id_sala, $horario_inicio, $horario_fim){
  
      $url = "http://localhost/desafio-fullstack/api/curso/create.php";
  
      // Parametros da requisição
      $data = http_build_query(array(
          'nome' =&gt; $nome,
          'id_professor' =&gt; $id_professor,
          'id_sala' =&gt; $id_sala,
          'horario_inicio' =&gt; $horario_inicio,
          'horario_fim' =&gt; $horario_fim
      ));
  
      $context = stream_context_create(array(
          'http' =&gt; array(
              'method' =&gt; 'POST',                    
              'header' =&gt; "Connection: close\r\n".
              "Content-type: application/x-www-form-urlencoded\r\n".
              "Content-Length: ".strlen($data)."\r\n",
              'content' =&gt; $data                               
          )
      ));
  
      $contents = file_get_contents($url, null, $context);            
      $resposta = json_decode($contents);  //Parser da resposta Json
      var_dump($resposta);
      die('antes do direcionamento dentro do criar curso');
  </code></pre>
  
  <p>Here I keep getting the message inside the <strong>if</strong> that checks if the <strong>$data->id_sala</strong> is empty. But when I send the same data using Postman, it's not empty. For the purpose of this first API, I wanna use only pure PHP. No external libs like <strong>Guzzle</strong> or anything like that. So, can someone give me a little help?</p>
  
  <p><strong>UPDATE:</strong>
  By changing the header on the call to send json format, I was able to send the data correctly to the API: </p>
  
  <pre><code>$opcoes = array(
          'http' =&gt; array(
              'header'  =&gt; "Content-type: application/json\r\n",
              'method'  =&gt; 'POST',
              'content' =&gt; json_encode($data)
          )
      );
      $context  = stream_context_create($opcoes);
      $retorno = file_get_contents($url, false, $context);
  </code></pre>
  
  <p>Thanks for the help.</p>