PHPWorker
Implements
- LimitedPHPApi
Index
Constructors
Properties
Methods
Constructors
constructor
Parameters
optionalrequestHandler: PHPRequestHandler
optionalmonitor: EmscriptenDownloadMonitor
Returns PHPWorker
Properties
absoluteUrl
documentRoot
Methods
public__internal_setRequestHandler
Parameters
requestHandler: PHPRequestHandler
Returns void
addEventListener
Parameters
eventType: request.end | request.error | runtime.initialized | runtime.beforedestroy
listener: PHPEventListener
Returns void
chdir
Parameters
path: string
Returns void
defineConstant
Parameters
key: string
value: null | string | number | boolean
Returns void
fileExists
Parameters
path: string
Returns boolean
internalUrlToPath
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
Parameters
path: string
Returns boolean
isFile
Parameters
path: string
Returns boolean
listFiles
Parameters
path: string
optionaloptions: ListFilesOptions
Returns string[]
mkdir
Parameters
path: string
Returns void
mkdirTree
Parameters
path: string
Returns void
mv
Moves a file or directory in the PHP filesystem to a new location.
Parameters
fromPath: string
toPath: string
Returns Promise<void>
onDownloadProgress
The onDownloadProgress event listener.
Parameters
callback: (progress: CustomEvent<ProgressEvent<EventTarget>>) => void
Returns Promise<void>
onMessage
Parameters
listener: MessageListener
Returns () => Promise<void>
Returns Promise<void>
pathToInternalUrl
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
Parameters
path: string
Returns Uint8Array
readFileAsText
Parameters
path: string
Returns string
removeEventListener
Parameters
eventType: request.end | request.error | runtime.initialized | runtime.beforedestroy
listener: PHPEventListener
Returns void
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 withcli()
.Parameters
request: PHPRequest
PHP Request data.
Returns Promise<PHPResponse>
rmdir
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
Parameters
request: PHPRunOptions
Returns Promise<PHPResponse>
setPrimaryPHP
Parameters
php: PHP
Returns Promise<void>
setSapiName
Parameters
newName: string
Returns void
unlink
Parameters
path: string
Returns void
writeFile
Parameters
path: string
data: string | Uint8Array
Returns void
A PHP client that can be used to run PHP code in the browser.