Playground API Client
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
The two methods you can use to run PHP code are:
In Node.js, you can also use the cli()
method to run PHP in a CLI mode.
The run() method
The request() method
Serves the request – either by serving a static file, or by dispatching it to the PHP runtime.
The request() method mode behaves like a web server and only works if
the PHP was initialized with a requestHandler
option (which the online
version of WordPress Playground does by default).
In the request mode, you pass an object containing the request information (method, headers, body, etc.) and the path to the PHP file to run:
const php = PHP.load('7.4', {
requestHandler: {
documentRoot: "/www"
}
})
php.writeFile("/www/index.php", `<?php echo file_get_contents("php://input");`);
const result = await php.request({
method: "GET",
headers: {
"Content-Type": "text/plain"
},
body: "Hello world!",
path: "/www/index.php"
});
// result.text === "Hello world!"
The request()
method cannot be used in conjunction with cli()
.
Customizing PHP.ini
The API client also allows you to change the php.ini file:
await setPhpIniEntries(client, {
display_errors: 'On',
error_reporting: 'E_ALL',
});