Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/235.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
主机上的PHP脚本在完成之前超时_Php_Loops_While Loop - Fatal编程技术网

主机上的PHP脚本在完成之前超时

主机上的PHP脚本在完成之前超时,php,loops,while-loop,Php,Loops,While Loop,我和one.com的支持人员谈过,他们说他们的服务器在50秒后超时 问题是,它会在脚本完成之前超时。如何使脚本循环直到完成 这是我的剧本: <?php //$id = $_GET['id']; //$content = file_get_contents("http://www.boligsiden.dk/salg/$id"); $con=mysqli_connect("danico.dk.mysql","danico_dk","password hidden","danic

我和one.com的支持人员谈过,他们说他们的服务器在50秒后超时

问题是,它会在脚本完成之前超时。如何使脚本循环直到完成

这是我的剧本:

    <?php

//$id = $_GET['id'];

//$content = file_get_contents("http://www.boligsiden.dk/salg/$id");

$con=mysqli_connect("danico.dk.mysql","danico_dk","password hidden","danico_dk");
// Check connection
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$result = mysqli_query($con,"SELECT * FROM elements");

$int = 0;


while($row = mysqli_fetch_array($result)) {

    if($int < 500 && $row['link'] != "," && $row['link'] != "") {

    $link = $row['link'];

    $content = file_get_contents("http://www.boligsiden.dk/salg/$link");

    preg_match('#"LatLng":{"Lat":(.*?),"Lng":#', $content, $match1);
    $lat = $match1[1];

  echo "<tr>";

  echo "<td>" . $lat . "</td>";
  echo "</tr>";

  $int = $int + 1;

    }

}

?>

您可以更改允许脚本运行的时间:

set_time_limit(300); // allow a maximum execution time of 5 minutes
警告

当PHP在安全模式下运行时,此函数无效。除了关闭安全模式或更改php.ini中的时间限制之外,没有其他解决方法

警告


小心这个选项。错误的使用会导致脚本永远不会结束。在脚本顶部设置一个明确的时间限制是最安全的方法。

就像其他答案所说的那样,您应该使用
设置时间限制(超时前int$seconds)
但无需将其放入循环:

set\u time\u limit(0)
切断任何超时(安全模式除外)

你应该添加一个缓存系统:一旦你用
file\u get\u contents()
重试了你的数据,你就可以把它保存到一个本地文件(比如
/cache/boligsiden/salg/$link.dat
),所以下次如果缓存是最近的(你决定“最近的”是什么意思),您将获得本地文件,而不是执行长时间的汇总
http
请求

一个解决方案:


@Arif\u suhail\u 123:if语句末尾实际上有一个
$int=$int+1
。然而,由于没有
else{break;}
,这在很大程度上是不相关的。@Arif_suhail_123
$int=0
首先,如果输入了
语句,则
,如果
语句中有一个增量
$int=$int+1
$int++
也会这样做),所以显然没有无限循环。@Andreas:set\u time\u limit()
的答案是正确的-但是当你说one.com服务器在50秒后超时时,这是否意味着它不能被覆盖?是的,它不能被覆盖。这个循环可以重复多少次?也许它可以被转移到后台任务中——这将是我通常的页面获取方法。
<?php
$con=mysqli_connect("danico.dk.mysql","danico_dk","password hidden","danico_dk");
if (mysqli_connect_errno())
{
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
    exit();
    // else, script will continue...
    // You could use a throw new Exception(...) instead
}

$result = mysqli_query($con,"SELECT * FROM elements WHERE `link`!=',' and `link`!='' LIMIT 500");
// Way easier to put the conditions on the query itself
// as your query is not reused anywhere else

set_time_limit(0);  // No timeout

while($row = mysqli_fetch_array($result))
{
    // No more if there since the query itself managed it faster
    $link = $row['link'];
    $content = file_get_contents("http://www.boligsiden.dk/salg/$link");
    preg_match('#"LatLng":{"Lat":(.*?),"Lng":#', $content, $match1);

    $lat = $match1[1];

    echo "<tr><td>$lat</td></tr>";
}

set_time_limit(30); // From now on, script will timeout again
// That line can be removed depending on what's coming next
// Aka if you still want the script to not timeout, remove that line
?>