title: Preserve data type of post fields when sending data using PHP and cURL
body: <p>I am aware that HTTP POST form data are sent to server-side as string values and that I would have to cast them to booleans or integers and so on. </p>
  
  <p>But my question is : <strong>Is it possible to POST data using cURL whilst preserving the types of values?</strong></p>
  
  <p>Eg: </p>
  
  <p>The following bit of PHP code prepares PHP array of data, where 'age' is an int and 'optin' is a boolean value and then sends this data to a test script using cURL via POST method.</p>
  
  <pre><code>$url = 'http://www.sometestserver.com/test/receive.php';
  
  $data = array (
      'name'  =&gt; "Firstname SecondName",
      'age'   =&gt; 25,
      'optin' =&gt; true
  );
  
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  curl_setopt($ch, CURLOPT_HEADER, false);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_POST, 1);
  curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/xml'));
  $response = curl_exec($ch);
  </code></pre>
  
  <p>And I have the following code on the test script which the above code posts the data to. The script simply reads whatever that comes in to it and var_dumps everything to a text file. </p>
  
  <pre><code>ob_start();
  var_dump($_POST);
  $data = ob_get_clean();
  file_put_contents(time() . ".txt", $data);
  </code></pre>
  
  <p>And the output of that is the following : </p>
  
  <pre><code>array(22) {
    ["name"]=&gt;
    string(21) "Firstname SecondName"
    ["age"]=&gt;
    string(2) "25"
    ["optin"]=&gt;
    string(1) "1"
  }
  </code></pre>
  
  <p>As you can see, all values were converted to type string, and the boolean true was converted to string "1". </p>
  
  <p>In another test, boolean false is sent as an empty string ''. </p>
  
  <p><strong>Is there any way of preserving the data type?</strong> 
  Maybe by setting a particular curl option? 
  or is this not possible at all? </p>
  
  <p>If cUrl ALWAYS convert boolean values to string, are there any hard and fast rules of what values to expect on the other end ? i.e. is bool true ALWAYS going to be string "1" and false ALWAYS going to be an empty string?</p>
  
  <p>Thanks for your help.  </p>