Using PHP Extensions
PHP extensions are provided as ESM modules.
You can pass an array as the sharedLibs
argument
to the constructor from Javascript to automatically generate an
ini file that loads your extensions.
import libxml from 'php-wasm-libxml';
import dom from 'php-wasm-dom';
const php = new PhpWeb({sharedLibs: [
,
libxml,
dom; ]})
You can also use the dynamicLibs
argument to get
similar behavior without automatically loading
the extension. A PHP script can call the dl()
function to dynamically load the extension at runtime:
import libxml from 'php-wasm-libxml';
import dom from 'php-wasm-dom';
const php = new PhpWeb({dynamicLibs: [
,
libxml,
dom; ]})
<?php
// Load the extension at runtime
dl('php-8.3-dom.so');
Dynamic Imports for Extensions
You can also load extensions modules dynamically:
// This will load both sqlite.so & php8.x-sqlite.so:
const php = new PhpWeb({sharedLibs: [
await import('https://unpkg.com/php-wasm-sqlite')
; ]})
Unfortunately, this notation is not available for Service
Workers, as they do not yet support dynamic
imports()
. Hopefully this will change soon.
Loading extensions manually
If you’re in an environment where ESM is unavailable, you can provide the extensions manually.
The first codeblock is shorthand for the second. Passing
ini: true
will automatically load the extension via
/php.ini
, passing ini: false
will wait
for a call to dl()
to do the lookup.
const php = new PhpWeb({sharedLibs: [ await import('https://unpkg.com/php-wasm-sqlite') ]});
Supporting libraries should not be loaed via
ini
.
const php = new PhpWeb({sharedLibs: [
{name: `php8.3-phar.so`,
url: `https://unpkg.com/php-wasm-sqlite/php8.3-sqlite.so`,
ini: true,
,
}
{ name: 'sqlite.so'
url: 'https://unpkg.com/php-wasm-sqlite/sqlite.so',
ini: false
}; ]})
Strings starting with /
, ./
,
http://
or https://
will be treated as
URLs:
const php = new PhpWeb({sharedLibs: [
`./php8.3-phar.so`
; ]})
Some extensions require supporting libraries. You can provide
URLs for those as sharedLibs
as well, just pass
ini: false
:
(name
is implied to be the last section of
the URL here.)
const php = new PhpWeb({sharedLibs: [
url: 'https://unpkg.com/php-wasm-sqlite/php8.3-sqlite.so', ini: true },
{ url: 'https://unpkg.com/php-wasm-sqlite/sqlite.so', ini: false },
{ ; ]})
Extension List
The following extensions may be loaded at runtime. This allows the shared extensions and their dependencies to be cached, reused, and selected à la carte for each application
gd
https://www.npmjs.com/package/php-wasm-gd
iconv
https://www.npmjs.com/package/php-wasm-iconv
intl
https://www.npmjs.com/package/php-wasm-intl
libxml
https://www.npmjs.com/package/php-wasm-libxml
xml
https://www.npmjs.com/package/php-wasm-xml
dom
https://www.npmjs.com/package/php-wasm-dom
simplexml
https://www.npmjs.com/package/php-wasm-simplexml
yaml
https://www.npmjs.com/package/php-wasm-libyaml
zip
https://www.npmjs.com/package/php-wasm-libzip
mbstring
https://www.npmjs.com/package/php-wasm-mbstring
openssl
https://www.npmjs.com/package/php-wasm-openssl
phar
https://www.npmjs.com/package/php-wasm-phar
sqlite
https://www.npmjs.com/package/php-wasm-sqlite
pdo-sqlite
https://www.npmjs.com/package/php-wasm-sqlite