Skip to main content

publicPHPRequestHandler

Handles HTTP requests using PHP runtime as a backend.

@example

Use PHPRequestHandler implicitly with a new PHP instance:

import { PHP } from '@php-wasm/web';

const php = await PHP.load( '7.4', {
requestHandler: {
// PHP FS path to serve the files from:
documentRoot: '/www',

// Used to populate $_SERVER['SERVER_NAME'] etc.:
absoluteUrl: 'http://127.0.0.1'
}
} );

php.mkdirTree('/www');
php.writeFile('/www/index.php', '<?php echo "Hi from PHP!"; ');

const response = await php.request({ path: '/index.php' });
console.log(response.text);
// "Hi from PHP!"
@example

Explicitly create a PHPRequestHandler instance and run a PHP script:

import {
loadPHPRuntime,
PHP,
PHPRequestHandler,
getPHPLoaderModule,
} from '@php-wasm/web';

const runtime = await loadPHPRuntime( await getPHPLoaderModule('7.4') );
const php = new PHP( runtime );

php.mkdirTree('/www');
php.writeFile('/www/index.php', '<?php echo "Hi from PHP!"; ');

const server = new PHPRequestHandler(php, {
// PHP FS path to serve the files from:
documentRoot: '/www',

// Used to populate $_SERVER['SERVER_NAME'] etc.:
absoluteUrl: 'http://127.0.0.1'
});

const response = server.request({ path: '/index.php' });
console.log(response.text);
// "Hi from PHP!"

Index

Constructors

constructor

  • The request handler needs to decide whether to serve a static asset or run the PHP interpreter. For static assets it should just reuse the primary PHP even if there’s 50 concurrent requests to serve. However, for dynamic PHP requests, it needs to grab an available interpreter. Therefore, it cannot just accept PHP as an argument as serving requests requires access to ProcessManager.


    Parameters

    Returns PHPRequestHandler

Properties

getFileNotFoundAction

getFileNotFoundAction: FileNotFoundGetActionCallback

processManager

processManager: PHPProcessManager

rewriteRules

rewriteRules: RewriteRule[]

Accessors

absoluteUrl

  • get absoluteUrl(): string
  • The absolute URL of this PHPRequestHandler instance.


    Returns string

documentRoot

  • get documentRoot(): string
  • The directory in the PHP filesystem where the server will look for the files to serve. Default: /var/www.


    Returns string

Methods

getPrimaryPhp

  • getPrimaryPhp(): Promise<PHP>
  • Returns Promise<PHP>

internalUrlToPath

  • internalUrlToPath(internalUrl: string): string
  • Converts an absolute URL based at the PHPRequestHandler to a relative path without the server pathname and scope.


    Parameters

    • internalUrl: string

      An absolute URL based at the PHPRequestHandler root.

    Returns string

    The relative path.

pathToInternalUrl

  • pathToInternalUrl(path: string): string
  • Converts a path to an absolute URL based at the PHPRequestHandler root.


    Parameters

    • path: string

      The server path to convert to an absolute URL.

    Returns string

    The absolute URL.

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().

    @example
    const output = await php.request({
    method: 'GET',
    url: '/index.php',
    headers: {
    'X-foo': 'bar',
    },
    body: {
    foo: 'bar',
    },
    });
    console.log(output.stdout); // "Hello world!"

    Parameters

    Returns Promise<PHPResponse>