PHP: Check if cURL is enabled.

This is a short guide on how to programmatically check whether cURL is enabled as a PHP extension or not. By checking to see if cURL is enabled, you can make your PHP code more portable by designing a fallback.

There are three popular approaches to this kind of check.

Does curl_init exist as a function?

The function curl_init allows us to initialize a cURL session. If it does not exist, then we can presume that the cURL module has not been loaded.

if(function_exists('curl_init') === false){
    //curl_init is not defined
    //cURL not enabled
}

In the code above, we used the PHP function function_exists to test whether curl_init is defined or not. If the function is not defined, we can presume that cURL has not been enabled and/or installed.

If curl_init is defined, then function_exists will return a boolean TRUE value.

Get loaded extensions.

Another approach is to use the PHP function get_loaded_extensions, which will return an array of all the PHP extensions / modules that have been loaded.

//Check if "curl" can be found in the array of loaded extensions.
if(in_array('curl', get_loaded_extensions())){
    //cURL module has been loaded
} else{
    //It has not been loaded. Use a fallback.
}

In the example above, we simply searched the array returned by get_loaded_extensions for the string “curl”. If the string “curl” cannot be found inside this array, then the extension has not been loaded and we can tell our code to use a fallback.

Yet another alternative is to use the function extension_loaded like so:

if(extension_loaded('curl')){
    //the extension has been loaded
}

You can choose which approach you think is best, as they all accomplish the same thing and the performance differences are pretty negligible at best.

Fallback.

If you are looking for a fallback to use when the cURL module has not been enabled, then you can check out the following guides that I wrote:

Hopefully, you found this guide to be useful!

Facebook Comments