Make HTTP/2 request with PHP

前端 未结 1 1192
慢半拍i
慢半拍i 2020-12-20 21:59

Is there a way to force PHP to make a HTTP2 connection to another server just to see if that server supports it?

I\'ve tried:

$options = stream_conte         


        
相关标签:
1条回答
  • 2020-12-20 22:42

    As far as I'm aware, cURL is the only transfer method in PHP that supports HTTP 2.0.

    You'll first need to test that your version of cURL can support it, and then set the correct version header:

    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 && strpos($response, "HTTP/2") === 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.";
    }
    
    0 讨论(0)
提交回复
热议问题