Playground API クライアント
PlaygroundClient オブジェクトは UniversalPHP インターフェースを実装しています。このインターフェースのすべてのメソッドは、Node.js および同一プロセス内の PHP インスタンスでも利用可能です( Playground は Web Worker 内で PHP を実行します)。
大まかに言うと、クライアントを使用して 3 種類の操作を実行できます。
- PHP コードの実行
PHP.iniのカスタマイズ- ファイルとディレクトリの管理
PHP コードの実行
PHP コードを実行するには、以下の 2 つの方法があります。
Node.js では、cli()メソッドを使用して PHP を CLI モードで実行することもできます。
run() メソッド
request() メソッド
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().
PHP.iniのカスタマイズ
この API クライアントを使用すると、php.iniファイルを変更することもできます。
await setPhpIniEntries(client, {
display_errors: 'On',
error_reporting: 'E_ALL',
});