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 ()?
1 answer
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
$curl = curl_init();), then the garbage collector will rest the old channel. Like so. - user207618