首页 > 解决方案 > 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

解决方案


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添加了一个新行。


推荐阅读