Edit report at https://bugs.php.net/bug.php?id=80931&edit=1
ID: 80931
Comment by: danack@php.net
Reported by: gilperon at gmail dot com
Summary: file_get_contents() hangs on PHP 8
Status: Verified
Type: Bug
Package: Streams related
Operating System: any
PHP Version: 8.0.3
Block user comment: N
Private report: N
New Comment:
Setting the protocol back to 1.0 with the protocol version option appears to make it work on 8.
I'll need to actually inspect the packets to have a deeper look.
> But I am pretty sure it will start bothering many other devs as they
> update their PHP to 8.x and start seeing their code breaking.
To set your expectation, if this is a bug on the remote server, then it's unlikely we would
write a hack around it. People can either use a workaround in their code, stay on PHP7, or ask the
person who owns that site to fix it. It's not feasible to put work arounds in php core for all
buggy servers out there.
<?php
$context = stream_context_create(array(
"http" => array(
"protocol_version" => "1.0"
)
));
$response = file_get_contents("http://ws.correios.com.br/calculador/CalcPrecoPrazo.aspx?nCdEmpresa=&sDsSenha=&sCepOrigem=11661690&sCepDestino=88070-480&nVlPeso=1&nCdFormato=1&nVlComprimento=25&nVlAltura=3&nVlLargura=25&sCdMaoPropria=N&nVlValorDeclarado=0&sCdAvisoRecebimento=N&nCdServico=04014&nVlDiametro=0&StrRetorno=xml",
0, $context);
echo "response length is " . strlen($response) . "\n";
Previous Comments:
------------------------------------------------------------------------
[2021-04-07 17:21:45] gilperon at gmail dot com
I just want to point that Correios (http://correios.com.br/), whose API I am using and which is the
one that makes file_get_contents dont work as expected, is the largest shipping
organization in Brazil (96% marketshare), and it's owned by the brazilian goverment. I am
pretty sure many many devs use this API with PHP and I am probably one of the few to face this
problem because, probably, many devs are still using older versions of PHP here in brazil. But I am
pretty sure it will start bothering many other devs as they update their PHP to 8.x and start seeing
their code breaking. Just a side note in case you think I am using a "special URL" only
created to make this problem happen (the URL I provided you is the official goverment end point of
their webhook).
------------------------------------------------------------------------
[2021-04-07 16:59:05] danack@php.net
Oh, the difference is PHP 7 set HTTP/1.0, PHP 8 does HTTP/1.1
"GET
/calculador/CalcPrecoPrazo.aspx?nCdEmpresa=&sDsSenha=&sCepOrigem=11661690&sCepDestino=88070-480&nVlPeso=1&nCdFormato=1&nVlComprimento=25&nVlAltura=3&nVlLargura=25&sCdMaoPropria=N&nVlValorDeclarado=0&sCdAvisoRecebimento=N&nCdServico=04014&nVlDiametro=0&StrRetorno=xml
HTTP/1.0\r\nHost: ws.correios.com.br\r\nConnection: close\r\n\r\n"
"GET
/calculador/CalcPrecoPrazo.aspx?nCdEmpresa=&sDsSenha=&sCepOrigem=11661690&sCepDestino=88070-480&nVlPeso=1&nCdFormato=1&nVlComprimento=25&nVlAltura=3&nVlLargura=25&sCdMaoPropria=N&nVlValorDeclarado=0&sCdAvisoRecebimento=N&nCdServico=04014&nVlDiametro=0&StrRetorno=xml
HTTP/1.1\r\nHost: ws.correios.com.br\r\nConnection: close\r\n\r\n"
------------------------------------------------------------------------
[2021-04-07 16:57:04] nikic@php.net
PHP 8 uses HTTP 1.1 instead of HTTP 1.0 by default, maybe that's related?
------------------------------------------------------------------------
[2021-04-07 16:54:23] danack@php.net
Somewhat astoundingly, the difference appears to be come from the remote server.
PHP7
recvfrom(3, "HTTP/1.1 200 OK\r\ncache-control: private\r\ncontent-type: text/xml;
charset=iso-8859-1\r\nexpires: Wed, 07 Apr 2021 16:38:47 GMT\r\nserver:
Microsoft-IIS/10.0\r\nx-aspnet-version: 4.0.30319\r\nset-cookie:
ASP.NET_SessionId=1et22nqgou4jk0ecrns5opu3; path=/; HttpOnly; SameSite=Lax\r\nx-powered-by:
ASP.NET\r\ndate: Wed, 07 Apr 2021 16:38:46 GMT\r\ncontent-length: 464\r\nconnection:
close\r\n\r\n<?xml version=\"1.0\" encoding=\"ISO-8859-1\"
?>\n<Servicos><cServico>
PHP8
recvfrom(3, "HTTP/1.1 200 OK\r\ncache-control: private\r\ncontent-type: text/xml;
charset=iso-8859-1\r\nexpires: Wed, 07 Apr 2021 16:40:02 GMT\r\nserver:
Microsoft-IIS/10.0\r\nx-aspnet-version: 4.0.30319\r\nset-cookie:
ASP.NET_SessionId=4wf1fkvdhyzzz1czsdoqdd01; path=/; HttpOnly; SameSite=Lax\r\nx-powered-by:
ASP.NET\r\ndate: Wed, 07 Apr 2021 16:40:02 GMT\r\ncontent-length: 464\r\n\r\n<?xml
version=\"1.0\" encoding=\"ISO-8859-1\" ?>\n<Servicos><cServico>
The response under PHP 7 includes a 'connection: close", the response under PHP 8
doesn't.
I can't currently see any difference in the way the connection is being made that would cause
that difference.
------------------------------------------------------------------------
[2021-04-07 15:37:25] cmb@php.net
I can confirm the reported behavior. The request is sent, the
response headers are read, but for some reason select(2) times out
under PHP 8.0, but not under PHP 7.4. After a few timeouts, the
response body is finally read, though.
------------------------------------------------------------------------
The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at
https://bugs.php.net/bug.php?id=80931
--
Edit this bug report at https://bugs.php.net/bug.php?id=80931&edit=1