php-mod / curl

This library provides an object-oriented and dependency free wrapper of the PHP cURL extension.
MIT License
328 stars 120 forks source link

Cannot send boolean value #7

Closed matsubo closed 9 years ago

matsubo commented 9 years ago

I passed following data to post method.

array(4) {
  ["amount"]=>
  int(900)
  ["currency"]=>
  string(3) "JPY"
  ["card"]=>
  string(28) "tok_SMJhkoibbHr9CYsv2wwcZ1Ik"
  ["capture"]=>
  bool(false)
}

However the value of capture is changed from false to 0.

string(67) "amount=900&currency=JPY&card=tok_SMJhkoibbHr9CYsv2wwcZ1Ik&capture=0"

This is caused http_build_query inside of post method. https://github.com/php-mod/curl/blob/master/src/Curl/Curl.php#L60

This case of scenario is missing in test cases.

amouhzi commented 9 years ago

Thank you @matsubo for the bug reporting. But as I know we can't post boolean data. All data is treated as strings. So boolean data will be converted to strings before to send them via POST. http_build_query convert false to 0, true to 1. If we suppose another function which converts them to "true" and "false", how can guest that were boolean and not a simple strings? Do you have an idea?

amouhzi commented 9 years ago

What you can do is something like that:

$capture = (bool) $_POST['capture'];
If your application is waiting for strings: "true" and "false", then you change your array to:
[
"amount" => 900
"currency" => "JPY"
"card" => "tok_SMJhkoibbHr9CYsv2wwcZ1Ik"
"capture" => $capture ? "true" : "false"
]

amouhzi commented 9 years ago

You can also use filters: http://php.net/manual/en/function.filter-var.php

So this is not a bug. This is an HTTP behavior.

matsubo commented 9 years ago

Yes. I noticed there is no variable type on HTTP. I was confused with JSON response data. Thank you for your kindly reply.