Reputation: 26281
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?
}
Upvotes: 11
Views: 8563
Reputation: 37018
You are missing the exit condition. Assuming it's a response from the remote script, the your code should be:
<?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 );
$rsp = false;
while (!$rsp) {
$rsp = curl_exec( $ch );
}
curl_close( $ch );
// Do something
Upvotes: 0