我试图从PHP中的输入流中读取,但有东西错误地删除了换行符。
test.php
<?php
$data = file_get_contents('php://input');
$entries = explode("n", $data);
print_r($entries);
?>
测试:
$ echo -e "123,a,b,cn456,d,e,fn" > test.txt
$ curl http://example.com/test.php --data @test.txt
Array
(
[0] => 123,a,b,c456,d,e,f
)
预期的输出应该是一个包含每一行新行的数组,但是我在数组中只得到一个元素。
我该如何阻止这种不正确的行为?这是个虫子吗?
PHP 7.4没有问题。cURL转换新行。
您可以在cURL命令中使用--data-binary
,以"在没有任何额外处理的情况下完全按照规定张贴数据">(来源(
echo -e "123,a,b,cn456,d,e,fn" > 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] =>
)
请注意,由于echo
添加了一条新行,因此有2个行尾。