Function curl_close is Deprecated in PHP 8.5

Function curl_close is Deprecated in PHP 8.5

Before PHP 8.0, the function curl_close was used to close the Curl resource created by using the curl_init function. Since PHP 8.0, the curl_close function has no effect because curl_init returns the CurlHandle object instead of a resource. The CurlHandle is automatically closed when it is no longer referenced or when it is explicitly destroyed.

Since PHP 8.5, the curl_close function emits a deprecation warning:

<?php

$ch = curl_init('https://httpbin.org/get');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$content = curl_exec($ch);
curl_close($ch);

Example will output:

Deprecated: Function curl_close() is deprecated since 8.5, as it has no effect since PHP 8.0 ...

In the previous example, a deprecation warning can be easily fixed by removing the curl_close function call. In this case, the CurlHandle will be automatically closed when $ch is no longer referenced, for example when the variable goes out of scope. We can also explicitly close the handle by calling unset($ch) or by assigning $ch = null.

<?php

$ch = curl_init('https://httpbin.org/get');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$content = curl_exec($ch);
unset($ch);

Leave a Comment

Cancel reply

Your email address will not be published.