【发布时间】:2023-03-13 04:40:01
【问题描述】:
我有一段 file_get_contents 代码,它工作正常,但有时源 URL 没有响应,因此我的页面在 60 秒内没有加载(默认超时后)。 如果源 URL 在 5 秒内没有响应,我想为 file_get_contents 设置 5 秒超时以执行其他操作。 这是我的代码:
<?php
$url = 'https://www.example.com';
$content = file_get_contents($url);
$first_step = explode( '<div class="js-price-value">' , $content );
$second_step = explode("</div>" , $first_step[1] );
$second_step[0]= str_replace( " ", "", $second_step[0]);
if (strlen(trim($second_step[0]))!==0) {
$price=$second_step[0];
echo $price;
}else {
echo '<div>something else</div>'
};
?>
在上面的代码之后我想要这样的东西:
if source url dose not respond in 5 seconds
{
do something else
}
【问题讨论】:
-
请选择一个答案,帮助有相同问题的用户找到解决方案。
标签: php file-get-contents php-ini