Parse a reply
<?php $p = new Resp3\Parser();$p->feed("*2\r\n+OK\r\n:42\r\n"); while ($p->hasNext()) { var_dump($p->next());}
The array reply comes out as ['OK', 42].
Verbatim strings
<?php $p = new Resp3\Parser();$p->feed("=15\r\ntxt:Some string\r\n=18\r\nmkd:# Hello world!\r\n"); $a = $p->next();$b = $p->next(); var_dump($a instanceof Resp3\VerbatimString);echo "a.type=$a->type a.value=$a->value\n";var_dump($b instanceof Resp3\VerbatimString);echo "b.type=$b->type b.value=$b->value\n";
Output:
bool(true)a.type=txt a.value=Some stringbool(true)b.type=mkd b.value=# Hello world!
Attributes
An attribute frame (|) belongs to the message that follows it. Read it with lastAttributes() after taking that message.
<?php $p = new Resp3\Parser();$p->feed("|1\r\n+key-popularity\r\n%2\r\n+a\r\n,0.1923\r\n+b\r\n,0.0012\r\n+after-attr\r\n"); $msg = $p->next();var_dump($msg); // string(10) "after-attr"var_dump($p->lastAttributes()); // ['key-popularity' => ['a' => 0.1923, 'b' => 0.0012]]
Push messages in queue mode
Redis sends push frames (>) out of band: client tracking invalidations and pubsub messages can arrive between two replies on the same connection. If you pipeline N commands and read N replies with next(), a push in the middle is returned in place of a reply and every later reply lands one slot off. Queue mode keeps replies and pushes apart.
<?php $bytes = ":1\r\n>2\r\n+message\r\n+hi\r\n:2\r\n"; $p = new Resp3\Parser(queuePushes: true);$p->feed($bytes); $replies = [];$pushes = []; while ($p->hasNext()) { $replies[] = $p->next(); // regular replies only}while ($p->hasPush()) { $pushes[] = $p->nextPush(); // Resp3\PushMessage, oldest first} var_dump($replies); // [1, 2]var_dump($pushes[0]->payload); // ['message', 'hi']
Handle protocol errors
Inside your read loop:
$p = new Resp3\Parser(); try { while ($p->hasNext()) { handle($p->next()); }} catch (Resp3\RedisException $e) { if ($e->prefix === 'PROTOCOL') { $p->reset(); // drop the connection too }}
Server errors are returned, not thrown. Route on $prefix:
As a fragment, inside your read loop:
$reply = $p->next(); if ($reply instanceof Resp3\RedisException) { match ($reply->prefix) { 'MOVED', 'ASK' => /* follow the redirect */ null, 'NOAUTH' => /* authenticate */ null, default => throw $reply, };}
amphp/redis adapter
The repository ships Resp3\Adapter\AmpRedisConnector, which routes an amphp/redis client through the C parser. This is examples/amphp_smoke.php from the repository. It needs a Redis or Valkey on 127.0.0.1:6379.
<?php declare(strict_types=1); require __DIR__ . '/../vendor/autoload.php'; use Amp\Redis\RedisConfig;use Amp\Socket\ConnectContext;use Resp3\Adapter\AmpRedisConnector; use function Amp\Redis\createRedisClient; if (!extension_loaded('resp3')) { fwrite(STDERR, "ext-resp3 not loaded; run with -d extension=./modules/resp3.so\n"); exit(1);} $config = RedisConfig::fromUri('redis://127.0.0.1:6379');$connector = new AmpRedisConnector( $config->getConnectUri(), (new ConnectContext())->withConnectTimeout($config->getTimeout()),); $client = createRedisClient($config, $connector); $client->delete('r3:smoke:str', 'r3:smoke:list', 'r3:smoke:hash', 'r3:smoke:counter'); $client->execute('SET', 'r3:smoke:str', 'hello');$got = $client->execute('GET', 'r3:smoke:str');assert($got === 'hello', "GET mismatch: " . var_export($got, true)); $missing = $client->execute('GET', 'r3:smoke:nonexistent');assert($missing === null, "expected null on missing key, got " . var_export($missing, true)); $incr = $client->execute('INCR', 'r3:smoke:counter');assert(is_int($incr), "INCR should return int, got " . gettype($incr)); $client->execute('RPUSH', 'r3:smoke:list', 'a', 'b', 'c');$range = $client->execute('LRANGE', 'r3:smoke:list', '0', '-1');assert($range === ['a', 'b', 'c'], "LRANGE mismatch: " . json_encode($range)); $client->execute('HSET', 'r3:smoke:hash', 'k1', 'v1', 'k2', 'v2');$hash = $client->execute('HGETALL', 'r3:smoke:hash');assert($hash === ['k1', 'v1', 'k2', 'v2'], "HGETALL mismatch: " . json_encode($hash)); $client->delete('r3:smoke:str', 'r3:smoke:list', 'r3:smoke:hash', 'r3:smoke:counter'); echo "amphp_smoke.php: OK (AmpRedisConnector -> Resp3\\Parser routed all replies)\n";
Run it with the extension loaded:
php -d extension=./modules/resp3.so examples/amphp_smoke.php
Fledge
fledge-fiber needs no adapter. Its Redis connection picks the extension parser automatically when the resp3 extension is loaded at version 0.2.0 or newer, and falls back to its pure-PHP RespParser otherwise. To check whether the extension is loaded:
var_dump(extension_loaded('resp3'), phpversion('resp3'));
See Fledge for the framework itself.