I have some code that that I only want to execute if curl_init() is present. I’m checking for it’s existence with this code:
if(function_exists(‘curl_init’))
{
$c = curl_init();
curl_setopt($c, CURLOPT_URL, ‘http://www.my-site.ext/’ . $data . ‘/’);
curl_setopt($c, CURLOPT_HEADER, 0);
curl_exec($c);
curl_close($c);
}The if() returns true, however the code inside throws this error:
Fatal error: Call to undefined function curl_init() in /var/www/wp-content/plugins/my-plugin-name/inc/myplugin_functions.php on line 15How can I properly check whether curl_init() exists?
……………………………………..
You can try is_callable() like this:
if(is_callable(‘curl_init’)){
//Code here
}
……………………………………..
try curl_exec instead curl_init, perhaps help
if(function_exists(‘curl_exec’))
{
// Code here
}
……………………………………..
yes, function_exists has worked for me in the past for the same cause…. are you sure that all curl related methods such as init setopt exec and close and the use of curl’s resource handler are within the if block?? more of your code would be helpful for a better answer..
……………………………………..
It would work properly, its your code you have inside the block that causes that problem.
All it does is returns TRUE if curl_init exists and is a function, FALSE otherwise. This function would be available if cURL is available, regardless of what curl_* function you put in.