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.
But my question is : Is it possible to POST data using cURL whilst preserving the types of values?
Eg:
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.
$url = 'http://www.sometestserver.com/test/receive.php';
$data = array (
'name' => "Firstname SecondName",
'age' => 25,
'optin' => 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);
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.
ob_start();
var_dump($_POST);
$data = ob_get_clean();
file_put_contents(time() . ".txt", $data);
And the output of that is the following :
array(22) {
["name"]=>
string(21) "Firstname SecondName"
["age"]=>
string(2) "25"
["optin"]=>
string(1) "1"
}
As you can see, all values were converted to type string, and the boolean true was converted to string "1".
In another test, boolean false is sent as an empty string ''.
Is there any way of preserving the data type? Maybe by setting a particular curl option? or is this not possible at all?
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?
Thanks for your help.