Skip to main content

WebPHPEndpoint

A PHP client that can be used to run PHP code in the browser.

Implements

  • Omit<IsomorphicLocalPHP, setSapiName | setPhpIniEntry | setPhpIniPath>

Index

Constructors

constructor

  • new WebPHPEndpoint(requestHandler: PHPRequestHandler<WebPHP>, monitor?: EmscriptenDownloadMonitor): WebPHPEndpoint
  • @inheritDoc

    Parameters

    • requestHandler: PHPRequestHandler<WebPHP>
    • optionalmonitor: EmscriptenDownloadMonitor

    Returns WebPHPEndpoint

Properties

absoluteUrl

absoluteUrl: string
@inheritDoc

documentRoot

documentRoot: string
@inheritDoc

Methods

addEventListener

  • addEventListener(eventType: request.end | request.error | runtime.initialized | runtime.beforedestroy, listener: PHPEventListener): void
  • @inheritDoc

    Parameters

    • eventType: request.end | request.error | runtime.initialized | runtime.beforedestroy
    • listener: PHPEventListener

    Returns void

chdir

  • chdir(path: string): void
  • Parameters

    • path: string

    Returns void

defineConstant

  • defineConstant(key: string, value: null | string | number | boolean): void
  • @inheritDoc

    Parameters

    • key: string
    • value: null | string | number | boolean

    Returns void

fileExists

  • fileExists(path: string): boolean
  • Parameters

    • path: string

    Returns boolean

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.

isDir

  • isDir(path: string): boolean
  • Parameters

    • path: string

    Returns boolean

listFiles

  • listFiles(path: string, options?: ListFilesOptions): string[]
  • Parameters

    • path: string
    • optionaloptions: ListFilesOptions

    Returns string[]

mkdir

  • mkdir(path: string): void
  • Parameters

    • path: string

    Returns void

mkdirTree

  • mkdirTree(path: string): void
  • Parameters

    • path: string

    Returns void

mv

  • mv(fromPath: string, toPath: string): Promise<void>
  • Moves a file or directory in the PHP filesystem to a new location.


    Parameters

    • fromPath: string

      The path to rename.

    • toPath: string

      The new path.

    Returns Promise<void>

onDownloadProgress

  • onDownloadProgress(callback: (progress: CustomEvent<ProgressEvent<EventTarget>>) => void): Promise<void>
  • The onDownloadProgress event listener.


    Parameters

    • callback: (progress: CustomEvent<ProgressEvent<EventTarget>>) => void

    Returns Promise<void>

onMessage

  • onMessage(listener: MessageListener): void
  • Parameters

    • listener: MessageListener

    Returns void

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.

readFileAsBuffer

  • readFileAsBuffer(path: string): Uint8Array
  • Parameters

    • path: string

    Returns Uint8Array

readFileAsText

  • readFileAsText(path: string): string
  • Parameters

    • path: string

    Returns string

removeEventListener

  • removeEventListener(eventType: request.end | request.error | runtime.initialized | runtime.beforedestroy, listener: PHPEventListener): void
  • @inheritDoc

    Parameters

    • eventType: request.end | request.error | runtime.initialized | runtime.beforedestroy
    • listener: PHPEventListener

    Returns void

request

  • request(request: PHPRequest): Promise<PHPResponse>
  • 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().


    Parameters

    • request: PHPRequest

      PHP Request data.

    Returns Promise<PHPResponse>

rmdir

  • rmdir(path: string, options?: RmDirOptions): Promise<void>
  • Removes a directory from the PHP filesystem.


    Parameters

    • path: string

      The directory path to remove.

    • optionaloptions: RmDirOptions

      Options for the removal.

    Returns Promise<void>

run

  • run(request: PHPRunOptions): Promise<PHPResponse>
  • Parameters

    • request: PHPRunOptions

    Returns Promise<PHPResponse>

setPhpIniEntry

  • setPhpIniEntry(key: string, value: string): void
  • Parameters

    • key: string
    • value: string

    Returns void

setPhpIniPath

  • setPhpIniPath(path: string): void
  • Parameters

    • path: string

    Returns void

setPrimaryPHP

  • setPrimaryPHP(php: WebPHP): Promise<void>
  • Parameters

    Returns Promise<void>

setSapiName

  • setSapiName(newName: string): void
  • Parameters

    • newName: string

    Returns void

unlink

  • unlink(path: string): void
  • Parameters

    • path: string

    Returns void

writeFile

  • writeFile(path: string, data: string | Uint8Array): void
  • Parameters

    • path: string
    • data: string | Uint8Array

    Returns void