php - php74错误地修改输入流
问题描述
我正在尝试从 PHP 中的输入流中读取数据,但是错误地删除了换行符。
测试.php
<?php
$data = file_get_contents('php://input');
$entries = explode("\n", $data);
print_r($entries);
?>
和测试:
$ echo -e "123,a,b,c\n456,d,e,f\n" > test.txt
$ curl http://example.com/test.php --data @test.txt
Array
(
[0] => 123,a,b,c456,d,e,f
)
预期的输出应该是一个包含每个新行的数组,但是我只得到数组中的一个元素。
我怎样才能停止这种不正确的行为?这是一个错误吗?
解决方案
PHP 7.4 没有问题。cURL 转换新行。
您可以--data-binary
在 cURL 命令中使用。
$ echo -e "123,a,b,c\n456,d,e,f\n" > test.txt
$ curl http://example.com/test.php --data-binary @test.txt
输出:
Array
(
[0] => 123,a,b,c
[1] => 456,d,e,f
[2] =>
[3] =>
)
请注意,有 2 个行尾,因为echo
添加了一个新行。
推荐阅读
- excel-formula - 解释使用带有附加条件的 countifs
- java - 如何在 Java 中创建 10 个从 1 到 10 且不重复的唯一随机数?
- jekyll - 使用 jekyll 在帖子顶部的响应式图像
- python - 我想循环时间,所以只要除了语句将命中时间将增加 10%
- loops - 如何在 Ansible 中将两个循环合二为一
- sql - 从两个不同的表计算增长百分比时除以零误差
- matlab - 相同对象名称的连续定义是否覆盖或附加 matlab 中的值
- python - 为什么 python 将 \\ 解释为“Bad escape”
- sql - 在我的过程中将两个不同的选择合并到同一个结果表中?
- r - 对于 R eg plot(GumbelDistribution) 中的极端分布