暂时禁用cURL以进行测试

I am writing a class that detects whether cURL is available, does one thing if it is, and another if it isn't. I therefore need to know how to disable cURL temporarily to test this class. I do not want to change the PHP INI file. Any ideas much appreciated.

Just wondering, Im writing an alternative for if cURL is unavailble, how likely is this? Am I wasting my time. Is cURL usually available?

I think the best option is to change your detection script to allow disabling it with a manual configuration.

You cannot disable function on the fly. You need to change php.ini for that.

AFAIK there is no way to do this at run time, because modules are loaded during PHP startup, before any of you code is executed. The only way to do it is by disabling (or rather, not enabling) an extension in php.ini. You probably can't even do that with cURL, as it will probably be compiled in, not dynamically loaded.

Having said that - why not just change the check to test your "if not available" code - presumably you have a block something like this:

if (curl_exists()) { //obviously there is no such function, but you must have some condition that determines this
  // Do stuff using curl
} else {
  // Do something horrible
}

well, just change it to this temporarily:

if (!curl_exists()) {
  // etc etc

Curl is enabled / disabled in your php.ini. You can't enable and disable it any other way.

Open php.ini find the below and put a semi colon before it to comment it out:

extension=php_curl.dll 

http://www.php.net/manual/en/function.dl.php

dl — Loads a PHP extension at runtime

bool dl ( string $library )

Loads the PHP extension given by the parameter library.

Use extension_loaded() to test whether a given extension is already available or not. This works on both built-in extensions and dynamically loaded ones (either through php.ini or dl()).

Warning: This function has been removed from some SAPI's in PHP 5.3.

<?php
// Example loading an extension based on OS
if (!extension_loaded('sqlite')) {  
    if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
        dl('php_sqlite.dll');
    } else {
        dl('sqlite.so');
    }
}
//this deals with sqlite but would be easy to figure out how to use it for cURL :)
?>

So you can comment out the loading of cURL extension in php.ini and then "dynamically load" it when needed.

HTH

probably the easiest way is by open file curl.ini, Im use ubuntu 12.04 and file located at

/etc/php5/apache2/conf.d/curl.ini

leave a comment by adding semicolon before extension=curl.so You can see the location of curl.ini through phpinfo ();

dont forget to restart the Apache

sudo service apache2 restart

Curl is available as long its extension is loaded (which is mostly by default).

You can check what curl extension provides by the following command:

php --re curl

which gives you list of functions, classes and its methods.

To temporary disable curl extension, you can run PHP with -n to simply ignore your php.ini, for example:

$ php -n -r "print_r(curl_version());"
Fatal error: Call to undefined function curl_version() in Command line code on line 1

Here is working example:

$ php -r "print_r(curl_version());"
Array
(
    [version_number] => 463623
...