X

PHP 測試 CURL 是否支援 HTTP 2

PHP 舊版或 CURL、SSL 等等太舊,可能會不支援 HTTP/2,要如何確認自己目前環境有支援 HTTP/2 呢?

PHP 測試 CURL 是否支援 HTTP 2

可以使用這隻程式來確認,執行此程式,出現 HTTP/2 support 就是支援,不然就不支援囉~

<?php
if (defined("CURL_VERSION_HTTP2") && (curl_version()["features"] & CURL_VERSION_HTTP2) !== 0) {
    $url = 'https://www.google.com/';
    $ch = curl_init();
    curl_setopt_array($ch, [
        CURLOPT_URL            => $url,
        CURLOPT_HEADER         => true,
        CURLOPT_NOBODY         => true,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_HTTP_VERSION   => CURL_HTTP_VERSION_2_0,
    ]);
    $response = curl_exec($ch);
    if ($response !== false && str_starts_with($response, 'HTTP/2'))
        echo "HTTP/2 support!";
    elseif ($response !== false)
        echo "No HTTP/2 support on server.";
    else
        echo curl_error($ch);
    curl_close($ch);
} else {
    echo "No HTTP/2 support on client.";
}
?>
Tsung: 對新奇的事物都很有興趣, 喜歡簡單的東西, 過簡單的生活.
Related Post