Php 出于测试目的暂时禁用cURL
我正在编写一个类来检测cURL是否可用,如果可用,做一件事,如果不可用,做另一件事。因此,我需要知道如何暂时禁用cURL来测试这个类。我不想更改PHP INI文件。非常感谢您的任何想法Php 出于测试目的暂时禁用cURL,php,curl,Php,Curl,我正在编写一个类来检测cURL是否可用,如果可用,做一件事,如果不可用,做另一件事。因此,我需要知道如何暂时禁用cURL来测试这个类。我不想更改PHP INI文件。非常感谢您的任何想法 只是想知道,我写了一个替代方案,如果cURL不可用,这种可能性有多大?我是在浪费时间吗。cURL通常可用吗?我认为最好的选择是更改检测脚本,允许通过手动配置禁用它 您不能动态禁用该功能。为此,您需要更改php.ini。我认为最好的选择是更改检测脚本,以便通过手动配置禁用它 您不能动态禁用该功能。为此,您需要更改p
只是想知道,我写了一个替代方案,如果cURL不可用,这种可能性有多大?我是在浪费时间吗。cURL通常可用吗?我认为最好的选择是更改检测脚本,允许通过手动配置禁用它
您不能动态禁用该功能。为此,您需要更改php.ini。我认为最好的选择是更改检测脚本,以便通过手动配置禁用它
您不能动态禁用该功能。为此,您需要更改php.ini。AFAIK在运行时无法执行此操作,因为模块是在php启动期间加载的,在执行任何代码之前加载的。唯一的方法是禁用(或者不启用)php.ini中的扩展。您甚至可能无法使用cURL实现这一点,因为它可能是在中编译的,而不是动态加载的 话虽如此,为什么不更改检查以测试“如果不可用”代码呢?假设您有这样一个块:
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
AFAIK在运行时没有办法做到这一点,因为模块是在PHP启动期间加载的,在执行任何代码之前。唯一的方法是禁用(或者不启用)php.ini中的扩展。您甚至可能无法使用cURL实现这一点,因为它可能是在中编译的,而不是动态加载的 话虽如此,为什么不更改检查以测试“如果不可用”代码呢?假设您有这样一个块:
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
Curl在
php.ini
中启用/禁用。您不能以任何其他方式启用和禁用它
打开php.ini
查找下面的内容,并在其前面放一个分号以注释它:
extension=php_curl.dll
Curl在
php.ini
中启用/禁用。您不能以任何其他方式启用和禁用它
打开php.ini
查找下面的内容,并在其前面放一个分号以注释它:
extension=php_curl.dll
dl-在运行时加载PHP扩展
布尔dl(字符串$library)
加载参数库给定的PHP扩展
使用extension_loaded()测试给定扩展是否已可用。这适用于内置扩展和动态加载的扩展(通过php.ini或dl())
警告:
该函数已从PHP5.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 :)
?>
dl-在运行时加载PHP扩展
布尔dl(字符串$library)
加载参数库给定的PHP扩展
使用extension_loaded()测试给定扩展是否已可用。这适用于内置扩展和动态加载的扩展(通过php.ini或dl())
警告:
该函数已从PHP5.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 :)
?>
最简单的方法可能是打开curl.ini文件,Im使用Ubuntu12.04和位于
/etc/php5/apache2/conf.d/curl.ini
在extension=curl.so之前添加分号以留下注释
您可以通过phpinfo()看到curl.ini的位置
别忘了重新启动Apache
sudo service apache2 restart
可能最简单的方法是打开curl.ini文件,Im使用Ubuntu12.04和位于
/etc/php5/apache2/conf.d/curl.ini
在extension=curl.so之前添加分号以留下注释
您可以通过phpinfo()看到curl.ini的位置
别忘了重新启动Apache
sudo service apache2 restart
只要加载了它的扩展名,Curl就可以使用(默认情况下大部分都是这样)
您可以通过以下命令检查curl
extension提供的功能:
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
...
只要加载了它的扩展名,Curl就可以使用(默认情况下大部分都是这样)
您可以通过以下命令检查curl
extension提供的功能:
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
...
请注意,Linux下的扩展名很可能是curl.so
,但处理它的方法保持不变。您知道在“child”php.ini文件中添加分号是否也会起作用吗?或者这只是一个注释,因此被忽略了?请注意,Linux下的扩展名很可能是curl.so
,但处理它的方法仍然是一样的。您知道在“child”php.ini文件中添加分号是否也会起作用吗?或者这只是一个注释,所以被忽略了?