php file_get_contents 连接何时关闭?

php file_get_contents when does connection close?

所以我明白 file_get_contents 一旦收到响应就会调用 fclose() 。但是,如果从未收到响应怎么办? php 的默认超时会处理超时响应并关闭连接还是保持打开状态?如果连接保持打开状态,我如何确保连接已关闭?还是我最好使用 cURL?

file_get_contents 在那里使用常规 stream context that you can even pass as 3rd parameter. You can either set a timeout option

$ctx = stream_context_create([
    'http' ['timeout' => 10]
]);

$content = file_get_contents('http://...', FALSE, $ctx);

当您未指定 http.timeout 上下文设置时,它将默认为 default_socket_timeout php.ini 设置(在大多数系统上默认设置为 60 秒)。