title: PHP sending POST request via curl
body: <p>I need to make a POST request to an external http API using curl. When I call the API using the curl command line, it returns the correct data. However, I'm struggling to get the right data when calling it from a php script.</p>
  
  <p>When I use the curl command from the terminal, I get the correct data: </p>
  
  <p>curl -i -H "Accept: application/json" -X POST -d "type_category_id=4" <a href="http://example.com/api/">http://example.com/api/</a></p>
  
  <p>What is the correct format for the CURLOPT_POSTFIELDS in my curl_setopts() in php?</p>
  
  <p>I tried the following (and failed):</p>
  
  <pre><code>&lt;?php
  $data = "type_category_id=4";
  
  $ch = curl_init('http://example.com/api/');
  curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
  curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_HTTPHEADER, array(
      'Content-Type: application/json',
      'Content-Length: ' . strlen($data))
  );
  
  $result = curl_exec($ch);
  ?&gt;
  </code></pre>