API request context
Call a JSON endpoint directly through the browser context and inspect its response.
This example uses the browser context's API request client to call a public JSON endpoint directly. It requires internet access.
Use it for setup, teardown, or API checks that do not need rendering.
Run it
php content/examples/api-request-context.php
Expected output:
Status: 200
Todo: <title>
The endpoint is public demo data, so the exact title can change if the service changes.
What it demonstrates
context->request()creates an API request client.get()performs an HTTP request without opening a page.json()reads the response body as an array.- API assertions verify the endpoint response; browser assertions verify the resulting UI behavior.
Go next
Source
api-request-context.php<?php
declare(strict_types=1);
/*
* This file is part of the community-maintained Playwright PHP project.
* It is not affiliated with or endorsed by Microsoft.
*
* (c) 2025-Present - Playwright PHP - https://github.com/playwright-php
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
require_once __DIR__.'/../../vendor/autoload.php';
use Playwright\Playwright;
use Playwright\Assertions\Expect;
$context = Playwright::chromium(['headless' => true]);
$request = $context->request();
$response = $request->get('https://jsonplaceholder.typicode.com/todos/1');
Expect::response($response)->toBeOK();
$data = $response->json();
echo 'Status: '.$response->status().PHP_EOL;
echo 'Todo: '.$data['title'].PHP_EOL;
$context->close();