Free tools Windows power users keep installed
One-click scans. No signup required.
iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more
To stream an LLM response from PHP to a browser, make PHP the relay: your browser requests your application, PHP calls the model API with streaming enabled, reads its event stream, and sends the useful output to the browser as Server-Sent Events (SSE). The browser can then display text as it arrives instead of waiting for the full answer.
“Streaming tokens” is convenient shorthand, not a promise that each network event contains exactly one model token or one word. Treat event boundaries as transport boundaries; the API sends incremental chunks or typed events.
How the PHP-to-browser streaming path works
- The browser opens a request to your PHP application. Your PHP endpoint prepares a browser-facing SSE response.
- PHP sends a streaming request to the model API. Keep the API credential on the server, not in browser code. The API overview links to authentication, endpoint, error, and rate-limit guidance: OpenAI API overview.
- PHP reads the upstream stream incrementally. It parses the API’s events or chunks as they arrive rather than waiting for the complete response.
- PHP emits browser-facing SSE frames. It can relay suitable text deltas and, if useful to the application, signal completion or failure.
- The browser renders arriving text. The user sees a partial answer while generation continues; the final status and any usage accounting may only be available later.
OpenAI documents HTTP streaming over SSE so applications can process the beginning of an output before generation finishes. See the streaming guide. The PHP relay pattern here applies that upstream stream to a browser-facing response; it is not a PHP-runtime-specific deployment recipe.
Do these 3 things before closing this tab:
1Clear out junk files and repair common Windows errors2Fix the driver behind crashes, sound loss and screen glitches3Repair Windows errors before they cause bigger problemsChoose the upstream streaming format
OpenAI’s Responses API and Chat Completions both support incremental output, but their stream structures differ. Pick one API surface and parse its documented format rather than treating every incoming line as plain text.
#1 Best Overall
| API surface | What PHP reads | Text extraction | Completion and interruption |
|---|---|---|---|
| Responses API | Typed events with semantic event names | Read incremental text from response.output_text.delta |
Lifecycle events indicate progress and completion, failure, or an incomplete response. See the Responses streaming event reference. |
| Chat Completions | Incremental completion chunks | Extract text from each streamed chunk according to the endpoint’s response structure | If the stream is interrupted or cancelled, the final usage chunk may not arrive. See the Chat Completions streaming reference. |
For either option, enable streaming in the upstream request. The official guide describes stream=true for HTTP SSE streaming and incremental processing. A text delta is content, not proof that the response has finished successfully; keep content delivery separate from lifecycle handling and final accounting.
What PHP should do with each incoming event
- Parse the upstream format. For Responses, inspect the event type and forward text from
response.output_text.delta. For Chat Completions, extract text from each chunk. Do not assume a chunk boundary corresponds to a word or model token. - Choose what the browser needs. You can send only text deltas, or translate meaningful completion and failure states into your own SSE event types. Avoid forwarding provider-specific payloads wholesale if the browser does not need them.
- Preserve useful terminal state. Distinguish successful completion from an upstream failure or incomplete response. Do not infer success merely because some text was already displayed.
- Keep credentials and upstream details server-side. The browser should connect to your PHP endpoint, not receive the provider secret.
- Account for partial answers. A user may have seen generated text even if the upstream request later fails or disconnects. Design the interface so partial output is not mistaken for a confirmed complete answer.
Make sure the stream actually reaches the browser
Sending SSE frames from PHP does not guarantee that the user sees them immediately. PHP output buffering, framework response handling, a web server, or a reverse proxy may hold data until more arrives or the request ends. Their exact behavior and controls depend on your PHP runtime and deployment stack, so there is no safe universal buffer or timeout setting to copy here.
Rank #2
Verify the full path in the environment you will deploy: the PHP application, framework, web server, and any proxy between the server and browser. Confirm that small incremental events arrive during generation, and check how the stack handles long-running requests, timeouts, and client disconnects. If output appears only at the end, investigate buffering along that path rather than assuming the model API stopped streaming.
What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.
Quick Recap
Rank #4
Handle disconnects, errors, and usage carefully
- Client disconnects: Detect when the browser connection is gone where your runtime and framework allow it, and stop or cancel upstream work when appropriate. The precise mechanism is stack-dependent.
- Upstream errors: Handle API errors separately from text events, and communicate a clear failure state to the browser if the connection is still available.
- Timeouts: Check limits at each layer in the request path. A timeout in PHP, the web server, or a proxy can end the browser stream even while upstream generation is in progress.
- Usage and final status: Do not treat displayed deltas as final usage data. Chat Completions may omit its final usage chunk after interruption or cancellation; Responses also has separate lifecycle events that indicate whether output completed or ended in another state.
A practical implementation checklist
- Choose Responses or Chat Completions and enable streaming in the upstream request.
- Parse the selected API’s documented event or chunk structure.
- Send the browser a
text/event-streamresponse and emit useful incremental updates. - Keep the API key on the PHP server.
- Represent completion and failure distinctly from text deltas.
- Test incremental delivery, buffering, disconnects, and timeouts across the actual deployment stack.
- Do not rely on a final usage event when a stream has been interrupted.
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

