Parse bytes from the command line
php -r ' $p = new Resp3\Parser(); $p->feed("*2\r\n+OK\r\n:42\r\n"); while ($p->hasNext()) { var_dump($p->next()); }'
If you built from source rather than using PIE, prefix the command with -d extension=./modules/resp3.so.
The read loop
$p = new Resp3\Parser($maxDepth = 100); // also: $maxBulk, $maxAggregateCount, $queuePushes $p->feed($bytes); // append bytes (no parse work)while ($p->hasNext()) { // drive the state machine; throws on protocol error $msg = $p->next(); // grab the buffered value // $msg may be: any scalar (incl. null/false from wire), array, or wrapper object} $attr = $p->lastAttributes(); // attributes (`|`) attached to the last value, or null$p->reset(); // wipe state and start fresh
feed() only appends to the buffer. hasNext() drives the state machine and returns true when a complete message is ready. next() returns it.
Splitting hasNext() and next() keeps "need more bytes" out of the return value. Every PHP scalar (null, false, 0, "") is a real RESP3 wire value, and none of them can be used as a sentinel.
You can feed partial data, for example one chunk per socket read. hasNext() returns false until a whole message is buffered.
Handle errors
Server errors (- and !) are returned as Resp3\RedisException, not thrown. Malformed wire bytes throw Resp3\RedisException with $prefix set to PROTOCOL, and the parser then stays in an error state until you call reset().
try { while ($p->hasNext()) { handle($p->next()); }} catch (Resp3\RedisException $e) { if ($e->prefix === 'PROTOCOL') { $p->reset(); // drop the connection too }}
Next steps
- API reference: every method, the type mapping and the limits.
- Examples: push queue mode and client adapters.