【问题标题】:How to check if curl is enabled or disabled如何检查 curl 是启用还是禁用
【发布时间】:2012-11-06 04:52:12
【问题描述】:

可能重复:
Writing a function in php

我正在使用以下代码

echo 'Curl: ', function_exists('curl_version') ? 'Enabled' : 'Disabled';

这可以启用或禁用它

但我想作为函数说函数名称是_iscurl

然后我可以在我的网站代码中的任何位置调用它

if (_iscurl()){
  echo "this is enabled"; // will do an action
}else{
  echo "this is disabled"; // will do another action
}

和我之前的问题check if allow_url_fopen is enabled or not几乎一样

【问题讨论】:

  • 为什么不使用function_exists('curl_version') 作为您的_iscurl() 函数?
  • 请参阅编写function 的手册,将您的单行代码装入其中,将echo 替换为return,然后将三元组去掉。

标签: php


【解决方案1】:

希望这会有所帮助。

<?php
    function _iscurl() {
        return function_exists('curl_version');
    }
?>

【讨论】:

  • function_exists 本身返回 truefalse。你可以只返回它的返回值。对于这个一行,不需要额外的 4 行代码。此外,您的函数没有结束 }!
  • 同意代码中的开销,但如果 else 语句不需要大括号,函数确实有一个 end } 作为一行。但也许糟糕的缩进让你犯了这个错误。
【解决方案2】:

您可以通过将这些代码放入 php 文件中进行检查。

<?php
if(in_array  ('curl', get_loaded_extensions())) {
    echo "CURL is available on your web server";
}
else{
    echo "CURL is not available on your web server";
}

var_dump(extension_loaded('curl'));

【讨论】:

    【解决方案3】:

    在你的项目中使用一个通用的可重用函数总是更好的,它返回是否加载了扩展。可以使用下面的函数来检查——

    function isExtensionLoaded($extension_name){
        return extension_loaded($extension_name);
    }
    

    用法

    echo isExtensionLoaded('curl');
    echo isExtensionLoaded('gd');
    

    【讨论】:

    • 假设您在一个大项目中工作,并且想要检查上述情况超过 30 到 40 次。然后您需要手动编写 30 到 40 次,突然之间您需要检查某些条件,然后您需要在项目中的所有 30 到 40 个位置搜索并放置条件。相反,如果您有可重用的函数,则可以将该条件放入函数中,从而避免搜索、替换或添加代码的开销。
    • 这就是重构工具的用途。但重点是:引入您不确定是否有必要的代码,我认为这是一个缺陷。因此,在我看来,您命名的原因是不这样​​做的原因。
    • 好主意。我已经开始包装所有原生 php 函数。我把它们收集在图书馆里。有人感兴趣吗?
    • @rosell.dk 我有兴趣请圈我。谢谢
    • @rosell.dk 哈哈!
    【解决方案4】:
    var_dump(extension_loaded('curl'));
    

    【讨论】:

    • 对于任何想要在不创建文件的情况下快速检查命令行的人:echo "&lt;?php var_dump(extension_loaded('curl')); ?&gt;" | php
    • 或:php -i | grep curlphp -r 'var_dump(extension_loaded("curl"));' ^^
    • 把它放到一个phpinfo文件的第一行:` `
    【解决方案5】:

    只需从function 退回您现有的支票即可。

    function _isCurl(){
        return function_exists('curl_version');
    }
    

    【讨论】:

    • 这行得通,并且被接受,但下面的答案是我认为不那么骇人听闻的,对阅读代码的其他人来说更清楚。如果我读到这篇文章,我可能会认为您是在专门检查是否可以找到 curl 版本,而不是查看是否加载了 curl。 extension_loaded('curl') 更直接。
    【解决方案6】:
    <?php
    
    // Script to test if the CURL extension is installed on this server
    
    // Define function to test
    function _is_curl_installed() {
        if  (in_array  ('curl', get_loaded_extensions())) {
            return true;
        }
        else {
            return false;
        }
    }
    
    // Ouput text to user based on test
    if (_is_curl_installed()) {
      echo "cURL is <span style=\"color:blue\">installed</span> on this server";
    } else {
      echo "cURL is NOT <span style=\"color:red\">installed</span> on this server";
    }
    ?>
    

    或者一个简单的 -

    <?
    phpinfo();
    ?>
    

    只要搜索 curl

    来源 - http://www.mattsbits.co.uk/item-164.html

    【讨论】:

      【解决方案7】:

      您始终可以创建一个新页面并使用phpinfo()。向下滚动到 curl 部分,看看它是否已启用。

      【讨论】:

        猜你喜欢
        • 2014-08-16
        • 2020-03-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-05-13
        • 1970-01-01
        • 2011-12-29
        相关资源
        最近更新 更多