Title: Playground API Client
Published: July 15, 2026
Last modified: July 16, 2026

---

# Playground API Client

## In this article

 * [Running PHP code](https://developer.wordpress.org/playground/developers/apis/javascript-api/playground-api-client/?output_format=md#running-php-code)
    - [The run() method](https://developer.wordpress.org/playground/developers/apis/javascript-api/playground-api-client/?output_format=md#the-run-method)
    - [The request() method](https://developer.wordpress.org/playground/developers/apis/javascript-api/playground-api-client/?output_format=md#the-request-method)
 * [Customizing PHP.ini](https://developer.wordpress.org/playground/developers/apis/javascript-api/playground-api-client/?output_format=md#customizing-php-ini)
 * [Managing files and directories](https://developer.wordpress.org/playground/developers/apis/javascript-api/playground-api-client/?output_format=md#managing-files-and-directories)
 * [Sending messages to JavaScript](https://developer.wordpress.org/playground/developers/apis/javascript-api/playground-api-client/?output_format=md#sending-messages-to-javascript)
 * [The cli() method](https://developer.wordpress.org/playground/developers/apis/javascript-api/playground-api-client/?output_format=md#the-cli-method)

[ Back to top](https://developer.wordpress.org/playground/developers/apis/javascript-api/playground-api-client/?output_format=md#wp--skip-link--target)

The `PlaygroundClient` object implements the `UniversalPHP` interface. All the methods
from that interface are also available in Node.js and same-process PHP instances(
Playground runs PHP in a web worker).

Broadly speaking, you can use the client to perform three types of operations:

 * Running PHP code
 * Customizing `PHP.ini`
 * Managing files and directories

## 󠀁[Running PHP code](https://developer.wordpress.org/playground/developers/apis/javascript-api/playground-api-client/?output_format=md#running-php-code)󠁿

The two methods you can use to run PHP code are:

 * [`run()`](https://developer.wordpress.org/playground/developers/apis/javascript-api/playground-api-client/?output_format=md#the-run-method)–
   runs PHP code and returns the output
 * [`request()`](https://developer.wordpress.org/playground/developers/apis/javascript-api/playground-api-client/?output_format=md#the-request-method)–
   makes an HTTP request to the website

In Node.js, you can also use the [`cli()`](https://developer.wordpress.org/playground/developers/apis/javascript-api/playground-api-client/?output_format=md#the-cli-method)
method to run PHP in a CLI mode.

### 󠀁[The run() method](https://developer.wordpress.org/playground/developers/apis/javascript-api/playground-api-client/?output_format=md#the-run-method)󠁿

### 󠀁[The request() method](https://developer.wordpress.org/playground/developers/apis/javascript-api/playground-api-client/?output_format=md#the-request-method)󠁿

## 󠀁[Customizing PHP.ini](https://developer.wordpress.org/playground/developers/apis/javascript-api/playground-api-client/?output_format=md#customizing-php-ini)󠁿

The API client also allows you to change the `php.ini` file:

    ```typescript
    await setPhpIniEntries(client, {
        display_errors: 'On',
        error_reporting: 'E_ALL',
    });
    ```

## 󠀁[Managing files and directories](https://developer.wordpress.org/playground/developers/apis/javascript-api/playground-api-client/?output_format=md#managing-files-and-directories)󠁿

The `client` object provides you with a low-level API for managing files and directories
in the PHP filesystem:

    ```typescript
    await client.mkdirTree('/wordpress/test');
    // Create a new PHP file
    await client.writeFile(
        '/wordpress/test/index.php',
        `<?php
         echo "Hello, world!<br/>";
         // List all the files in current directory
         print_r(glob(__DIR__ . '/*'));
      `
    );
    // Create files named 1, 2, and 3
    await client.writeFile('/wordpress/test/1', '');
    await client.writeFile('/wordpress/test/2', '');
    await client.writeFile('/wordpress/test/3', '');
    // Remove the file named 1
    await client.unlink('/wordpress/test/1');
    // Navigate to our PHP file
    await client.goTo('/test/index.php');
    ```

For a complete list of these methods, refer to the `PlaygroundClient` interface.

## 󠀁[Sending messages to JavaScript](https://developer.wordpress.org/playground/developers/apis/javascript-api/playground-api-client/?output_format=md#sending-messages-to-javascript)󠁿

You can pass messages from PHP to JavaScript using the `post_message_to_js()` function.
It accepts one argument:

 * `$data` (string) – Data to pass to JavaScript.

For example, here’s how you would send a message with a JSON-encoded post ID and
title:

    ```language-TypeScript
    const php = new PHP(await loadNodeRuntime('8.3'));

    php.onMessage(
        // The data is always passed as a string
        function (data: string) {
            // Let's decode and log the data:
            console.log(JSON.parse(data));
        }
    );

    // Now that we have a listener in place, let's
    // dispatch a message:
    await php.runStream({
        code: `<?php
            post_message_to_js(
                json_encode([
                    'post_id' => '15',
                    'post_title' => 'This is a blog post!'
                ])
            );
        `,
    });

    // You will see the following output in the console:
    // { post_id: '15', post_title: 'This is a blog post!' }
    ```

## 󠀁[The cli() method](https://developer.wordpress.org/playground/developers/apis/javascript-api/playground-api-client/?output_format=md#the-cli-method)󠁿

In Node.js, you also have access to the `cli()` method that runs PHP in a CLI mode:

    ```typescript
    // Run PHP in a CLI mode
    client.cli(['-r', 'echo "Hello, world!";']);
    // Outputs "Hello, world!"
    ```

Once `cli()` method finishes running, the PHP instance is no longer usable and should
be discarded. This is because PHP internally cleans up all the resources and calls`
exit()`.

First published

July 15, 2026

Last updated

July 16, 2026

Edit article

[ Improve it on GitHub: Playground API Client ](https://raw.githubusercontent.com/WordPress/wordpress-playground/trunk/packages/docs/site/docs/developers/06-apis/javascript-api/03-playground-api-client.md)

Changelog

[ See list of changes: Playground API Client ](https://developer.wordpress.org/playground/developers/apis/javascript-api/playground-api-client/?output_format=md#)

[  Previous: `remote.html` vs `index.html`](https://developer.wordpress.org/playground/developers/apis/javascript-api/index-html-vs-remote-html/)

[  Next: Blueprints JSON and the API Client](https://developer.wordpress.org/playground/developers/apis/javascript-api/blueprint-json/)