我需要一些帮助我试图终止我的脚本,如果它达到60秒,如果它无法连接到服务器,但我总是抓住60秒限制执行时间。我认为我的代码无效
<?php
$time_limit = 60;
set_time_limit ($time_limit);
if(isset($_GET['comm'])){
$command = $_GET['comm'];
$host = "xxx.xx.xxx.xx";
$port = xxxx;
$start_time = time();
$message = $command;
$socket = socket_create(AF_INET, SOCK_STREAM,0) or die("Could not create socket\n");
while(!@socket_connect($socket, $host, $port)){
if ((time() - $start_time) >= $time_limit)
{
socket_close($socket);
die("Connection timed out.\n");
}
sleep(1);
continue;
}
socket_write($socket, $message, strlen($message)) or die("Could not send data to server\n");
$resultserv = socket_read ($socket, 1024) or die("Could not read server response\n");
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => 'http://tomysite/receive.php',
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => array(
'recrespond' => $resultserv
)
));
$resp = curl_exec($curl);
curl_close($curl);
echo $resultserv;
socket_close($socket);
}
?>
提前谢谢。
答案 0 :(得分:0)
您需要添加ini_set('max_execution_time', 59);
答案 1 :(得分:0)
设置$ time_limit = 55,因为PHP执行时间为60秒将在脚本执行之前启动。如果你需要它运行60秒,我会使用相同的脚本,但使用ini_set()将执行时间增加到65秒;