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 AMPHP/Hpack

に公開

First, download it using Composer.

composer require amphp/hpack

Try using HPackNghttp2, which is an FFI wrapper class for nghttp2. On Debian, install libnghttp2-dev using the following command.

sudo apt install libnghttp2-dev
<?php
require __DIR__ . '/vendor/autoload.php';

$headers = [
    [":scheme", "https"],
    [":authority", "example.org"],
    [":path", "/"],
    ["user-agent", "libnghttp2"],
    ["accept-encoding", "gzip, deflate"]
];

$output = "\x87\x41\x88\x2f\x91\xd3\x5d\x05\x5c\xf6\x4d".
          "\x84\x7a\x87\xa0\xd1\xd5\x34\xe9\x4d\x62\x90";

$hpack = new Amp\Http\Internal\HPackNghttp2;

var_dump(
  $output === $hpack->encode($headers),
  $headers === $hpack->decode($output, 4096)
);

Discussion