The below script will operate indefinitely and will be initiated by using php myscript.php
.
http://example.com/longpolling.php will only respond if it has something to communicate to php myscript.php
, and the below curl request will timeout before longpolling.php will reach its time limitation.
Should I close and reopen the curl connection each loop, or keep it open indefinitely.
<?php
// php myscript.php
$options=[
CURLOPT_URL=>'http://example.com/longpolling.php',
CURLOPT_RETURNTRANSFER=>true,
CURLOPT_CONNECTTIMEOUT => 300,
CURLOPT_TIMEOUT=> 300
];
$ch = curl_init();
curl_setopt_array( $ch, $options );
while (true) {
$rsp = curl_exec( $ch );
// Do something
//curl_close( $ch ); //should I close and reopen?
}
curl_close
in while loop, you have to again initialized curl. because curl_init, Initializes a new session and return a cURL handle for use with the curl_setopt(), curl_exec(), and curl_close() functions. – Krongoldusleep()
in the while loop. If you can't put it in, again, the protocol does not fit your needs. – Heterotypic