开发者

How to check if curl has support for ssl?

开发者 https://www.devze.com 2023-02-28 16:41 出处:网络
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:

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 usi开发者_如何学编程ng 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)


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)


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
?>


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


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();

?>


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.";
}


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

function getScheme()
{
    if (extension_loaded('openssl')) {
        return 'https';
    }

    return 'http';
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号