dqkyz02602
2011-04-24 21:59
浏览 275
已采纳

如何检查curl是否支持ssl?

How can I check if php curl was compiled with ssl and has support for ssl? I mean, a simple check to see if curl is available would be:

if(extension_loaded('curl'))

but how do I check if curl also has support for ssl? I need this check because ssl support is needed to using oauth2 - based API and I need some way to quickly check if client's php is able to the oauth2 before actually using it (and failing)

图片转代码服务由CSDN问答提供 功能建议

如何检查php curl是否使用ssl编译并支持ssl? 我的意思是,一个简单的检查 看看curl是否可用将是:

if(extension_loaded('curl'))

但是如何检查curl是否也有支持 对于ssl? 我需要这个检查,因为使用基于oauth2的API需要ssl支持,我需要一些方法来快速检查客户端的php是否能够在实际使用它之前能够使用oauth2(并且失败)

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

6条回答 默认 最新

  • dongzhenqi2015 2011-04-24 22:32
    已采纳

    See http://www.php.net/manual/en/function.curl-version.php

    $version = curl_version();
    $ssl_supported= ($version['features'] & CURL_VERSION_SSL);
    

    In $version['features'] you have a features bitmask. Through an and operation between this bitmask and the proper constant you can check if a feture is enabled.

    Possible constants are:

    CURL_VERSION_IPV6 (integer)

    CURL_VERSION_KERBEROS4 (integer)

    CURL_VERSION_SSL (integer)

    CURL_VERSION_LIBZ (integer)

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dongyo1818 2011-04-24 22:24

    Check the output of stream_get_wrappers() if https is in the list you're good to go.

    评论
    解决 无用
    打赏 举报
  • dongsigan2636 2011-04-24 22:33

    AFAIK cURL doesn't have a way to check if SSL is enabled or disabled, testing it and catching would let you know for sure.

    You could try testing this, to see if it works. (I don't have a server without SSL to test on..)

    <?php
    $v = curl_version(); 
    print $v['ssl_version']; // I get: OpenSSL/0.9.8 - I presume you won't if it is not enabled
    ?>
    
    评论
    解决 无用
    打赏 举报
  • dongmei9961 2012-06-07 13:34

    If you put this code in a PHP file, you will be able to see all of the details for PHP in the browser:

    <? php
    
    phpinfo();
    
    ?>
    
    评论
    解决 无用
    打赏 举报
  • dpl57372 2015-01-31 09:43

    If the extension openssl is loaded, then cURL can use it.

    function getScheme()
    {
        if (extension_loaded('openssl')) {
            return 'https';
        }
    
        return 'http';
    }
    
    评论
    解决 无用
    打赏 举报
  • douxian6008 2015-07-29 22:05

    I am not sure if it is good idea to go with curl_version()['ssl_version'], (e.g. if (stripos(curl_version()['ssl_version'], "openssl") !== false) { ) as curl says here http://curl.haxx.se/docs/faq.html#Does_curl_work_build_with_other it may use other ssl library than OpenSSL (which does not have anything to do with that separated openssl extension, curl has its own openssl library) so as described here http://curl.haxx.se/libcurl/c/curl_version_info.html it appears better to go with CURL_VERSION_SSL bitmask check rather than curl_version()['ssl_version']. Note that not all of those constants stated on official cURL website are available in php, but only these four constants:

    [CURL_VERSION_IPV6] => 1
    [CURL_VERSION_KERBEROS4] => 2
    [CURL_VERSION_SSL] => 4
    [CURL_VERSION_LIBZ] => 8
    

    I tested this on Windows by disabling "openssl" extension in php.ini and noticed curl has nothing to do with that separated openssl extension but it has its own openssl, in other word, disabling openssl extension does not affect on $v['ssl_version'];. So if you want to check if curl has support for ssl, you should not rely on existence of that separated openssl extension and above I explained you should not rely on curl_version()['ssl_version'] neither. The only reliable way is CURL_VERSION_SSL bitmask checking:

    if (!curl_version()['features'] & CURL_VERSION_SSL) {
        echo "SSL is not supported with this cURL installation.";
    }
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题