【发布时间】:2017-11-27 05:47:23
【问题描述】:
在向另一个脚本发布 curl 帖子时,有时我可能想发送 null 作为某些键的值:
$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();
但是,数据在收到时似乎已经发生了变化。对 $_POST 进行 vardump 显示“时间”值现在是一个空字符串:
array(3) {
["userID"]=>
string(1) "0"
["questionText"]=>
string(29) "Can you answer this question?"
["time"]=>
string(0) ""
}
这里发生了什么?为什么一切都是字符串?我怎样才能坚持打字?
【问题讨论】:
-
在请求数组中根本不设置属性会怎样?
-
当然不会在 $_POST 数组中。
-
AFAIK 对网站的任何查询都将转换为文本,并且无法在文本中表示 null,因此最终您的帖子数据可能如下所示:
userID=0&questionText=Can you answer this question?&time= -
HTTP 仅传输文本。因此,无法准确表示空值。为什么它会给你带来问题?接收端需要区分空字符串和NULL吗?
-
在 HTTP 请求中,您只会收到字符串。您必须将值类型映射到服务器端的数据库字段。我建议使用 ORM 框架。