Search code examples
phpcurlhidesuppresserror-suppression

how to test if curl is available in PHP without errors


I am trying to detect if curl is installed using PHP in a script run from the command line. I tried the following:

if(@function_exists('curl_version')){
...
}

and

error_reporting(E_ERROR);
ini_set('display_errors', '0');

if(is_callable('curl_init')){
...
}

but in both cases I get this message:

PHP Warning:  PHP Startup: Unable to load dynamic library '/usr/local/lib/php/extensions/no-debug-zts-20121212/curl.so' - /usr/local/lib/php/extensions/no-debug-zts-20121212/curl.so: cannot open shared object file: No such file or directory in Unknown on line 0

I would prefer to hide the error message, but it appears that the @ and the error_reporting don't work. Is there a different way to suppress this message?


Solution

  • you could check your installed extionsions

        $needed_extensions = array('curl',  '... other extionsions to check');
        $missing_extensions = array();
        foreach ($needed_extensions as $needed_extension) {
            if (!extension_loaded($needed_extension)) {
                $missing_extensions[] = $needed_extension;
            }
        }
        if (count($missing_extensions) > 0) {
            echo 'This software needs the following extensions, please install/enable them: ' . implode(', ', $missing_extensions) . PHP_EOL;
            exit(1);
        }
    

    '