Skip to main content

Overview

This example demonstrates streaming HTTP responses for real-time data processing. It shows how to handle server-sent events (SSE) and incremental data streams.

Example

<?php
require 'examples/boot.php';

use Cognesy\Http\Creation\HttpClientBuilder;
use Cognesy\Http\Data\HttpRequest;
use Cognesy\Http\Data\HttpResponse;
use Cognesy\Http\Stream\ArrayStream;

// Basics: request a streamed response and iterate chunks.
// Use Mock driver to simulate SSE-like stream.

$client = (new HttpClientBuilder())
    ->withMock(function ($mock) {
        $mock->addResponse(
            HttpResponse::streaming(
                statusCode: 200,
                headers: ['Content-Type' => 'text/event-stream'],
                stream: ArrayStream::from(["hello\n", "from\n", "stream\n"]),
            ),
            url: 'https://api.example.local/stream',
            method: 'GET'
        );
    })
    ->create();

$request = new HttpRequest(
    url: 'https://api.example.local/stream',
    method: 'GET',
    headers: ['Accept' => 'text/event-stream'],
    body: '',
    options: ['stream' => true],
);

foreach ($client->withRequest($request)->stream() as $chunk) {
    echo $chunk; // handle streamed data incrementally
}
?>

Key Features

  • Streaming Responses: Handle real-time data as it arrives
  • Server-Sent Events: Support for SSE protocol
  • Memory Efficient: Process large responses incrementally
  • Mock Streaming: Test streaming without real network connections