php - php74 erroniously modifying the input stream

I am trying to read from the input stream in PHP, but something is erroneously removing newlines.
test.php
<?php
$data = file_get_contents('php://input');
$entries = explode("\n", $data);
print_r($entries);
?>
And the test:
$ 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
)
The expected output should be an array containing each of the new lines, however I only ever get a single element in the array.
How can I stop this incorrect behavior? Is this a bug?
Answer
Solution:
There is no problem with PHP 7.4. cURL converts the new lines.
You could use--data-binary
in your cURL command.
$ echo -e "123,a,b,c\n456,d,e,f\n" > test.txt
$ curl http://example.com/test.php --data-binary @test.txt
Output:
Array
(
[0] => 123,a,b,c
[1] => 456,d,e,f
[2] =>
[3] =>
)
Note that there is 2 end-of-line, becauseecho
adds a new line.
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: trying to access array offset on value of type bool in
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.