我正在编写一个类,它检测cURL是否可用,如果可用则做一件事,如果不可用则做另一件事。因此,我需要知道如何暂时禁用cURL来测试这个类。我不想更改PHP INI文件。如有任何意见,不胜感激。
只是想知道,我写一个替代如果cURL是不可用的,这有多大可能?我是在浪费时间吗?cURL通常可用吗?
Curl在您的php.ini
中启用/禁用。你不能以任何其他方式启用或禁用它。
打开php.ini
,找到下面的内容,并在其前面加一个分号以注释出来:
extension=php_curl.dll
在运行时没有办法做到这一点,因为模块是在PHP启动期间加载的,在任何代码执行之前。唯一的方法是禁用(或者不启用)php.ini中的扩展。你可能甚至不能在cURL中这样做,因为它可能会被编译,而不是动态加载。
话虽如此,为什么不改变检查来测试你的"if not available"代码-假设你有一个这样的块:
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
}
暂时改成:
if (!curl_exists()) {
// etc etc
我认为最好的选择是更改您的检测脚本,允许通过手动配置禁用它。
你不能在飞行中禁用功能。
http://www.php.net/manual/en/function.dl.php
dl -在运行时加载PHP扩展
bool dl (string $library)
加载参数库给出的PHP扩展。
使用extension_loaded()来测试给定的扩展是否已经可用。这适用于内置扩展和动态加载的扩展(通过php.ini或dl())。
:在PHP 5.3中,这个函数已经从一些SAPI中删除了。
<?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 :)
?>
所以你可以注释掉php.ini中cURL扩展的加载,然后"动态加载";
HTH
可能最简单的方法是打开file curl.ini,我使用的是ubuntu 12.04,文件位于
/etc/php5/apache2/conf.d/curl.ini
在extension=curl.so
前添加分号留下评论您可以通过phpinfo ();
不要忘记重新启动Apache
sudo service apache2 restart
Curl是可用的,只要它的扩展加载(这大部分是默认的)。
您可以通过以下命令检查curl
扩展提供的内容:
php --re curl
给出了函数、类及其方法的列表。
要临时禁用curl扩展,您可以运行带有-n
的PHP来忽略php.ini
,例如:
$ php -n -r "print_r(curl_version());"
Fatal error: Call to undefined function curl_version() in Command line code on line 1
下面是工作示例:
$ php -r "print_r(curl_version());"
Array
(
[version_number] => 463623
...