Make HTTP/2 request with PHP Make HTTP/2 request with PHP curl curl

Make HTTP/2 request with PHP


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