最近弄一些一定要支援 HTTP/2 的服務,但是內建的 PHP cURL library 卻不支援!!
所以如果沒有把握你的 PHP 是否支援的話,可以用下面的語法驗證一下喔!!
 
defined('CURL_VERSION_HTTP2') || define('CURL_VERSION_HTTP2', 65536);
defined('CURL_HTTP_VERSION_2_0') || define('CURL_HTTP_VERSION_2_0', 3);
defined('CURL_HTTP_VERSION_2') || define('CURL_HTTP_VERSION_2', CURL_HTTP_VERSION_2_0);
defined('CURLPIPE_NOTHING') || define(CURLPIPE_NOTHING, 0);
defined('CURLPIPE_HTTP1') || define('CURLPIPE_HTTP1', 1);
defined('CURLPIPE_MULTIPLEX') || define('CURLPIPE_MULTIPLEX', 2);

if (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 && strpos($response, "HTTP/2.0") === 0) {
        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.";
}
Facebook 討論區載入中...