Through ajax I execute php-code, with the help of curl I log in and execute the code. Such ajax requests can be a lot in a day. What can happen if I do not use curl_close ()?

  • 2
    It seems nothing, with the end of the script execution the descriptors are also closed. But who needs to keep the garbage in memory for all the time? - user207618
  • @Other Thank you) - gam1et22
  • @Other, and the open connection can not be reused? - Qwertiy
  • @Qwertiy, open will connect with where it is open. If a new assignment is made on the descriptor ( $curl = curl_init(); ), then the garbage collector will rest the old channel. Like so. - user207618
  • @Other, but you can not do it? Or not? - Qwertiy

1 answer 1

Upon completion of the script all resources will be closed automatically.

If you do not call curl_close , then the connection will remain open and available to other keep-alive requests from this script, respectively, within the time that the remote party is ready to wait for other requests. Therefore, if you made a request, received an answer - and more you don’t need anything from the remote machine - it’s better to call curl_close to avoid wasting a connection.

But if you need to perform several queries in a row to one remote machine, then it makes sense to do one curl_init first and then reuse this descriptor. Calling curl_close explicitly prohibits the reuse of the connection and a new connection will be created.

Those. view code

 $ch = curl_init(); foreach(...) { curl_setopt_array($ch, ...); $result = curl_exec($ch); ... } curl_close($ch); 

It will save some time spent on setting up a TCP connection, and for HTTPS it will save TLS, which is even more interesting.

Relevant note on habrahabr