`file_get_contents()` 有逾時機制嗎?

Patricia Arquette
發布: 2024-11-14 21:40:02
原創
442 人瀏覽過

Does `file_get_contents()` have a Timeout Mechanism?

Does File_Get_Contents() Timeout?

File_get_contents() is a powerful tool for retrieving content from remote URLs. However, concerns arise regarding potential timeouts when dealing with lengthy processes.

Does File_Get_Contents() Have a Timeout?

Fortunately, file_get_contents() does have a timeout mechanism. By default, the timeout is set by the default_socket_timeout ini-setting, which is configured to 60 seconds. This means that if the content cannot be retrieved within 60 seconds, a timeout exception will be thrown.

Customizing the Timeout

The default timeout can be customized to suit specific needs. Two methods can be used:

  • Ini Setting:
ini_set('default_socket_timeout', 900); // 900 Seconds (15 Minutes)
登入後複製
  • Stream Context:
$ctx = stream_context_create(array(
    'http' => array(
        'timeout' => 1200 // 1200 Seconds (20 Minutes)
    )
));

echo file_get_contents('http://example.com/', false, $ctx);
登入後複製

By setting a higher timeout, users can ensure that file_get_contents() waits longer before triggering a timeout.

以上是`file_get_contents()` 有逾時機制嗎?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板