Search code examples
phphttp2

How to make HTTP/2 requests via API


How to make HTTP/2 requests to an API with PHP?

I know HTTP/2 is more complicated with the binary encoded request/response and multiplexing

Can anyone provide a link to a tutorial or an example how to handle the requests with HTTP/2?


Solution

  • https://www.reddit.com/r/PHP/comments/301ncp/http2_support_for_curl_merged/

    This simply means that you can optionally tell curl_*() to communicate with the remote server via HTTP/2.0. If the remote server doesn't support the protocol the two parties will speak HTTP/1.1 as before. There are two scenarios in PHP userland where you can benefit from the use of HTTP/2.0 in a curl client:

    1. You're retrieving multiple resources from the same site concurrently over the same connection; you may experience performance improvement by avoiding the "head-of-line" blocking problem because HTTP/2.0 allows the multiplexing of multiple transfers concurrently over the same TCP connection.
    2. You're connecting via HTTPS (h2). Encrypted h2 sessions are negotiated as part of the TLS handshake via the TLS ALPN extension. This means you can establish the crypto session with fewer TCP roundrips via HTTP/2.0 than you'd be able to do with HTTP/1.1 (a performance benefit).

    cURL will handle the work for you, essentially.