1

When making a curl post to another script, sometimes I may want to send null as the value for certain keys:

$postdata = array(
        'userID'        => 0,
        'questionText'  => "Can you answer this question?",
        'time'          => null);

$req = curl_init($url);
curl_setopt($req, CURLOPT_POST, true);
curl_setopt($req, CURLOPT_POSTFIELDS, $postdata);
curl_setopt($req, CURLOPT_RETURNTRANSFER, false);
curl_setopt($req, CURLOPT_VERBOSE, true); 
$data = curl_exec($req);
curl_close();

However, the data seems to have changed by the time it is received. Doing a vardump on $_POST reveals that the 'time' value is now an empty string:

array(3) {
    ["userID"]=>
    string(1) "0"
    ["questionText"]=>
    string(29) "Can you answer this question?"
    ["time"]=>
    string(0) ""
}

What's going on here? Why is everything a string? How can I perserve the typing?

8
  • What happens when you don't set the attribute at all in the request array? Commented Nov 27, 2017 at 5:49
  • It won't be in the $_POST array, of course. Commented Nov 27, 2017 at 5:51
  • AFAIK any query to a website will be converted to text and there's no way to represent null in a text so in the end your post data might look like this: userID=0&questionText=Can you answer this question?&time= Commented Nov 27, 2017 at 5:52
  • 1
    HTTP transfers text only. Therefore a null value cannot be accurately represented. Why is it causing a problem for you? Do you need to differentiate between empty string and NULL on the receiving end? Commented Nov 27, 2017 at 6:03
  • 1
    In an HTTP request you will only ever receive strings. You have to map value types to database fields on the server side. I recommend to use an ORM framework. Commented Nov 28, 2017 at 0:24

1 Answer 1

1

As comments to the original question state, HTTP requests will only ever provide strings.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.