Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,11 @@ You can pass messages from PHP to JavaScript using the `post_message_to_js()` fu

For example, here's how you would send a message with a JSON-encoded post ID and title:

```ts
const php = await PHP.load('8.0');
```TypeScript
import { PHP } from '@php-wasm/universal';
import { loadNodeRuntime } from '@php-wasm/node';

const php = new PHP(await loadNodeRuntime('8.3'));

php.onMessage(
// The data is always passed as a string
Expand All @@ -90,17 +93,18 @@ php.onMessage(

// Now that we have a listener in place, let's
// dispatch a message:
await php.run({
await php.runStream({
code: `<?php
post_message_to_js(
json_encode([
'post_id' => '15',
'post_title' => 'This is a blog post!'
])
));
);
`,
});

console.log(await output.stdoutText);
// You will see the following output in the console:
// { post_id: '15', post_title: 'This is a blog post!' }
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,11 @@ Você pode enviar mensagens do PHP para o JavaScript usando a função `post_mes

Por exemplo, veja como enviar uma mensagem com um ID e título de post codificados em JSON:

```ts
const php = await PHP.load('8.0');
```TypeScript
import { PHP } from '@php-wasm/universal';
import { loadNodeRuntime } from '@php-wasm/node';

const php = new PHP(await loadNodeRuntime('8.3'));

php.onMessage(
// Os dados são sempre passados como string
Expand All @@ -123,8 +126,8 @@ php.onMessage(
);

// Agora que temos um listener, vamos
enviar uma mensagem:
await php.run({
//enviar uma mensagem:
await php.runStream({
code: `<?php
post_message_to_js(
json_encode([
Expand Down
4 changes: 2 additions & 2 deletions packages/php-wasm/node/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ import { loadNodeRuntime } from '@php-wasm/node';

const php = new PHP(await loadNodeRuntime('8.3'));

const output = await php.run({
const output = await php.runStream({
code: '<?php phpinfo(); ?>',
});

console.log(response.text);
console.log(await output.stdoutText);
```

## Attribution
Expand Down
33 changes: 21 additions & 12 deletions packages/php-wasm/web/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,37 @@ This package ships WebAssembly PHP binaries and the JavaScript API optimized for
Here's how to use it:

```js
import { PHP } from '@php-wasm/web';
import { PHP, PHPRequestHandler } from '@php-wasm/universal';
import { loadWebRuntime } from '@php-wasm/web';

// PHP.load() calls import('php.wasm') internally
// loadWebRuntime() calls import('php.wasm') and import('icudt74l.dat') internally.
// Your bundler must resolve import('php.wasm') as a static file URL.
// If you use Webpack, you can use the file-loader to do so.
const php = await PHP.load('8.0', {
requestHandler: {
documentRoot: '/www',
},
const php = new PHP(await loadWebRuntime('8.3'));

let response;

php.writeFile('/test.php', `<?php echo "Hello, World!"; ?>`);

// Run a script directly:
response = await php.runStream({
scriptPath: '/test.php',
});

// Create and run a script directly
php.mkdirTree('/www');
console.log(await response.stdoutText);

php.mkdir('/www');
php.writeFile('/www/index.php', `<?php echo "Hello " . $_POST['name']; ?>`);
await php.run({ scriptPath: './index.php' });

// Or use the familiar HTTP concepts:
const response = await php.request({
const handler = new PHPRequestHandler({ phpFactory: async () => php });

response = await handler.request({
method: 'POST',
url: '/index.php',
data: { name: 'John' },
url: 'index.php',
body: { name: 'John' },
});

console.log(response.text);
```

Expand Down
Loading