如何检查curl是否支持ssl?

发布于 2024-11-02 21:06:07 字数 219 浏览 0 评论 0原文

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

if(extension_loaded('curl'))

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

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

末骤雨初歇 2024-11-09 21:06:08

我不确定使用 curl_version()['ssl_version'] 是否是个好主意,(例如 if (stripos(curl_version()['ssl_version'], "openssl") !== false) { )
正如curl在这里所说 http://curl.haxx.se/docs/faq.html#Does_curl_work_build_with_other
它可能使用 OpenSSL 之外的其他 ssl 库(与分离的 openssl 扩展没有任何关系,curl 有自己的 openssl 库)
如此处所述 http://curl.haxx.se/libcurl/c/curl_version_info.html 似乎使用 CURL_VERSION_SSL 位掩码检查比 curl_version()['ssl_version'] 更好。请注意,并非官方 cURL 网站上声明的所有常量都可以在 php 中使用,但只有这四个常量:

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

我通过在 php.ini 中禁用“openssl”扩展在 Windows 上进行了测试,并注意到curl 与分离的 openssl 扩展无关但它有自己的 openssl,换句话说,禁用 openssl 扩展不会影响 $v['ssl_version'];。因此,如果你想检查curl是否支持ssl,你不应该依赖于那个分离的openssl扩展的存在,上面我解释过你也不应该依赖curl_version()['ssl_version']。唯一可靠的方法是 CURL_VERSION_SSL 位掩码检查:

if (!curl_version()['features'] & CURL_VERSION_SSL) {
    echo "SSL is not supported with this cURL installation.";
}

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.";
}
深海夜未眠 2024-11-09 21:06:08

如果将此代码放入 PHP 文件中,您将能够在浏览器中看到 PHP 的所有详细信息:

<? php

phpinfo();

?>

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

?>
山人契 2024-11-09 21:06:07

请参阅 http://www.php.net/manual/en/function。 curl-version.php

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

在 $version['features'] 中,您有一个功能位掩码。通过该位掩码和适当的常量之间的“与”运算,您可以检查某个功能是否已启用。

可能的常量有:

CURL_VERSION_IPV6(整数)

CURL_VERSION_KERBEROS4(整数)

CURL_VERSION_SSL(整数)

CURL_VERSION_LIBZ(整数)

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)

旧梦荧光笔 2024-11-09 21:06:07

AFAIK cURL 没有办法检查 SSL 是否启用或禁用,测试它并捕获会让您确定。

您可以尝试测试一下,看看它是否有效。 (我没有没有 SSL 的服务器可供测试..)

<?php
$v = curl_version(); 
print $v['ssl_version']; // I get: OpenSSL/0.9.8 - I presume you won't if it is not enabled
?>

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
?>
↙厌世 2024-11-09 21:06:07

检查 stream_get_wrappers() 的输出,如果 https 在列表中,那么您就可以开始了。

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

听,心雨的声音 2024-11-09 21:06:07

如果加载了 openssl 扩展,则 cURL 可以使用它。

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

    return 'http';
}

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

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

    return 'http';
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文