I have a PHP script "A" that starts another PHP script "B", which runs up to 5 hours. I use the curl function for that. But my problem is that script "A" does not sure hold the connection to the script "B". I have changed the max_execution_time, timeout, socket-timeout, etc... but nothing helps.
Do I need to send a header to the script "B" with curl or something?
$curl_header[] = "Accept: text/xml,application/xml,application/xhtml+xml,text
/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
$curl_header[] = "Cache-Control: max-age=0";
$curl_header[] = "Connection: keep-alive";
$curl_header[] = "Keep-Alive: 84600";
$url = 'http://test.de/test_B.php';
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)');
curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $curl_header);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 300);
curl_setopt($ch, CURLOPT_TIMEOUT, 84600);
curl_setopt($ch, CURLOPT_NOSIGNAL, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $url);
$result = curl_exec($ch);
$curl_errno = curl_errno($ch);
curl_close($ch);
`
It sets how long your server should wait for new requests from clients. A value between 7 to 10 seconds is usually ideal. With higher traffic this value can go extremely higher to make sure there is no frequent TCP connection re-initiated.
A keepalive is a signal sent from one device to another to maintain a connection between the two devices. This may be between a client and a server, but it could apply to any number of devices or technologies.
HTTP keep-alive, a.k.a., HTTP persistent connection, is an instruction that allows a single TCP connection to remain open for multiple HTTP requests/responses. By default, HTTP connections close after each request.
If the script "B" takes long time to finish it could be useful to put during the execution some echo "something"; flush();
that mantain the connection alive.
It happens to me recently on a similar execution.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With