Sometimes we need to run long running script on server but our server doesn’t allow that due to small execution time and we see Connection Timeout error. But still we can run that script.Today i am going to explain the logic how can we do this.
In php there is a function stream_set_blocking
If mode is FALSE, the given stream will be switched to non-blocking mode, and if TRUE, it will be switched to blocking mode. This affects calls like fgets() and fread() that read from the stream. In non-blocking mode an fgets() call will always return right away while in blocking mode it will wait for data to become available on the stream.

Steps to remove Connection Timeout error

 
$file = __DIR__ . '/CommandFunction.php';
$cmd = "php -r \"require '$file'; RunCommandFunction($arg1,'$arg2');\"";
$pipe = popen($cmd, 'r');
if (empty($pipe)) {
 throw new Exception("Unable to open pipe for command '$cmd'");
}
stream_set_blocking($pipe, false);
while (!feof($pipe)) {
 $output = fread($pipe, 1024);
 print_r($output);
 sleep(1);
 flush();
}
pclose($pipe);

Then you can create a function in CommandFunction.php and add the long running scripts into this function something like this

function RunCommandFunction($arg1,$arg2){
     // long running scripts here
     

     // end script
     echo "success";
}
http://codingbin.com/wp-content/uploads/2017/04/Capture1-64a8af9cd47ef9725249e93a9f64512d6827a51710a4363afaf18f96705bb56f.jpghttp://codingbin.com/wp-content/uploads/2017/04/Capture1-64a8af9cd47ef9725249e93a9f64512d6827a51710a4363afaf18f96705bb56f-150x150.jpgManoj DhimanphpserverphpSometimes we need to run long running script on server but our server doesn't allow that due to small execution time and we see Connection Timeout error. But still we can run that script.Today i am going to explain the logic how can we do this. In php there is...coding, php, wordpress, yii, cakephp, and  jquery

Are you want to get implementation help, or modify or extend the functionality of this script? Submit paid service request