0

I need some help I am trying to terminate my script if it reaches to 60 seconds if it can't connect to the server,but I always caught by 60 seconds limit execution time.I think my code is not working.

<?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);

}

?>

Thank you in advance.

kelly123
  • 29
  • 5
  • http://stackoverflow.com/questions/5164930/fatal-error-maximum-execution-time-of-30-seconds-exceeded you also clearly can't set a timeout at 60 when that is the php time limit as well it would need to be at 59. – michael.schuett Sep 23 '14 at 02:18

2 Answers2

0

You would need to add ini_set('max_execution_time', 59);

Avinash Babu
  • 6,171
  • 3
  • 21
  • 26
0

set the $time_limit = 55 since php execution time of 60 seconds is going to start before your script does. If you need it to run exactly for 60 seconds I would use the same script but increase the execution time to 65 seconds using ini_set();

michael.schuett
  • 4,248
  • 4
  • 28
  • 39
  • I add this ini_set('max_execution_time', 59);,then $time_limit = 55; set_time_limit ($time_limit);but the script did not terminate – kelly123 Sep 23 '14 at 03:08
  • don't use set_time_limit. you are saying your script can only run for 55 seconds when you do this and it overrides the ini_set(); – michael.schuett Sep 23 '14 at 03:10
  • I get caught of Fatal error: Maximum execution time of 59 seconds exceeded – kelly123 Sep 23 '14 at 03:26
  • I am guessing some of the other function calls are taking quite so time before you even hit the first connection. to test this set the timeout of the script to 300 seconds with ini_set and see if it still times out. If so we will have to start looking into a different solution. – michael.schuett Sep 23 '14 at 03:57
  • fatal error Maximum execution time of 60 seconds exceeded pointed this line $resultserv = socket_read ($socket, 1024) or die("Could not read server response\n"); – kelly123 Sep 23 '14 at 04:18
  • http://stackoverflow.com/questions/18732778/how-to-set-time-out-at-socket-read-in-php may help you. How much data are you reading in from the stream? it seems like it is a very large amount. – michael.schuett Sep 23 '14 at 04:24