10

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?
}
user1032531
  • 24,767
  • 68
  • 217
  • 387
  • if you use `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. – Kumar Oct 31 '16 at 12:29
  • @Kumar Exactly. Are there any memory/performance/reliability reasons to do so? – user1032531 Oct 31 '16 at 12:30
  • @apokryfos I am not saying it is right to do so, but it seems to work perfect leaving it open. Is it documented that it is "supposed to handle a single request"? – user1032531 Oct 31 '16 at 12:32
  • @user1032531 it's more of a semantic statement rather than any practical limitation. Two cURL handles are essentially the same size so keeping one open or closing it and opening another one shouldn't make any difference. – apokryfos Oct 31 '16 at 12:36
  • I use Guzzle instead of using curl directly. Try it https://github.com/guzzle/guzzle , it does all the closing and opening for you – DevJ3rry Oct 31 '16 at 12:41
  • The whole thing is wrong imo. I would use sockets without curl and instead of http I'd use something that fits better. At least put a small `usleep()` in the while loop. If you can't put it in, again, the protocol does not fit your needs. – Daniel W. Oct 31 '16 at 12:45
  • @DanFromGermany Good point, however, giving the existing and immutable networking configuration, I do not believe sockets are as appropriate. – user1032531 Oct 31 '16 at 12:48
  • 1
    @user1032531 http requests are made through sockets, you can't say sockets are not appropriate. sockets are just lower level = more flexible. You could utilize UDP and send packets connectionless for example. – Daniel W. Oct 31 '16 at 12:52
  • @DanFromGermany Good point again :) I suspected maybe so, but wasn't sure. Thanks for correcting me. – user1032531 Oct 31 '16 at 13:36

2 Answers2

6

If the URLs are on the same server reusing the handle will lead to an increase in performance. cURL will reuse the same TCP connection for each HTTP request to the server.

Here is also a nice benchmark for this issue.

Community
  • 1
  • 1
secelite
  • 1,353
  • 1
  • 11
  • 19
0

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
Alex Blex
  • 34,704
  • 7
  • 48
  • 75