iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🐕

[PHP] Using HTTP/2 without TLS (h2c) with AMPHP HTTP Client

に公開
2024/07/08

By specifying the HTTP version in the Request object, it automatically supports h2c.

<?php
# h2c test server
# https://zenn.dev/masakielastic/articles/530f9751697ed6

require __DIR__ . '/vendor/autoload.php';

use Amp\Http\Client\HttpClientBuilder;
use Amp\Http\Client\Request;

$request = new Request("http://localhost:8000");
$request->setProtocolVersions(['2']);

$client = HttpClientBuilder::buildDefault();
$response = $client->request($request);

var_dump(
  $response->getStatus(),
  $response->getHeaders(),
  $response->getBody()->buffer(),
  $response->getProtocolVersion()
);

Discussion