温馨提示×

如何设置file_get_contents的超时时间

PHP
小樊
95
2024-08-15 05:23:35
栏目: 编程语言

要设置file_get_contents的超时时间,可以使用Context参数,其中包含一个timeout选项,可以指定超时时间。下面是一个示例代码:

$url = 'https://www.example.com';
$options = array(
    'http' => array(
        'timeout' => 5 // 设置超时时间为5秒
    )
);

$context = stream_context_create($options);
$data = file_get_contents($url, false, $context);

if ($data === false) {
    echo "请求超时";
} else {
    echo $data;
}

在上面的示例中,我们设置了超时时间为5秒,如果请求在5秒内未完成,将会输出"请求超时"。您可以根据自己的需求调整超时时间。

0