With @platformatic/php-node
you can run PHP applications within the same
process as a Node.js application, allowing for communication between Node.js
and PHP without any network connection in the middle. This allows for some
interesting possibilities, like running Wordpress with a Next.js frontend.
Presently support is provided for x64 Linux and both x64 and arm64 macOS. More platforms will come as needs arise. Please open an issue if we're missing a platform you would like supported!
PHP dynamically links against several system libraries. These must be installed as listed below:
sudo apt-get update
sudo apt-get install -y libssl-dev libcurl4-openssl-dev libxml2-dev \
libsqlite3-dev libonig-dev re2c
brew install openssl@3 curl sqlite libxml2 oniguruma
npm install @platformatic/php-node
import { Php, Request } from '@platformatic/php-node'
const php = new Php()
const request = new Request({
url: 'http://example.com/foo/bar',
headers: {
'X-Test': ['Hello, from Node.js!']
}
})
const response = await php.handleRequest(request)
console.log(response.body.toString())
config
{Object} Configuration objectargv
{String[]} Process arguments. Default: []docroot
{String} Document root for PHP. Default: process.cwd()throwRequestErrors
{Boolean} Throw request errors rather than returning responses with error codes. Default: false
- Returns: {Php}
Construct a new PHP instance to which to dispatch requests.
import { Php } from '@platformatic/php-node'
const php = new Php({
argv: process.argv,
docroot: process.cwd()
})
request
{Request} A request to dispatch to the PHP instance.- Returns: {Promise}
When the request completes, the returned promise will resolve with the response object. Request processing is handled by the NodePlatform worker pool to avoid blocking the Node.js thread.
import { Php, Request } from '@platformatic/php-node'
const php = new Php()
const request = new Request({
url: 'http://example.com/foo/bar'
})
const response = await php.handleRequest(request)
console.log(response.body.toString())
request
{Request} A request to dispatch to the PHP instance.- Returns: {Response}
Requests may also be processed synchronously, though this is not recommended as it will block the Node.js thread for the entire life of the PHP request.
This may be useful for one-off scripts. It's only included because it's trivial to do so, but it's not recommended for use within HTTP requests.
import { Php, Request } from '@platformatic/php-node'
const php = new Php()
const request = new Request({
url: 'http://example.com/foo/bar'
})
const response = php.handleRequestSync(request)
console.log(response.body.toString())
input
method
{String} HTTP method Default:GET
url
{String} Full request URLheaders
{Object} HTTP request headers. Each must be an array of stringsbody
{Buffer|UInt8Array} Request body
- Returns: {Request}
Construct a request which may be dispatched to a PHP instance.
import { Request } from '@platformatic/php-node'
const request = new Request({
method: 'POST',
url: 'http://example.com/foo/bar',
headers: {
'Content-Type': ['application/json']
},
body: Buffer.from(JSON.stringify({
hello: 'world'
}))
})
- {String}
The HTTP method to use when dispatching this request.
import { Request } from '@platformatic/php-node'
const request = new Request({
url: 'http://example.com/foo/bar',
})
console.log(request.method) // GET
- {String}
The URL to use when dispatching this request.
import { Request } from '@platformatic/php-node'
const request = new Request({
url: 'http://example.com/foo/bar',
})
console.log(request.url) // http://example.com/foo/bar
- {Headers}
The HTTP headers to use when dispatching this request.
import { Request } from '@platformatic/php-node'
const request = new Request({
url: 'http://example.com/foo/bar',
})
console.log(request.headers) // [Headers]
- {Buffer}
The body to use when dispatching this request.
import { Request } from '@platformatic/php-node'
const request = new Request({
url: 'http://example.com/foo/bar',
body: Buffer.from('Hello, world!')
})
console.log(request.body.toString()) // Hello, world!
input
{Object} Response values.status
{Number} HTTP Response status codeheaders
{Object} HTTP Response headers. Each must be an array of stringsbody
{Buffer} HTTP Response bodylog
{String} Log output of this request
- Returns: {Response}
Responses may be constructed manually. This is mainly just for testing, but may have other uses, like short-circuiting the PHP instance run entirely in certain cases.
import { Response } from '@platformatic/php-node'
const response = new Response({
status: 500,
headers: {
'Content-Type': ['application/json']
},
body: Buffer.from(JSON.stringify({
error: 'bad stuff'
}))
})
- {Number}
The HTTP status code included in the response.
import { Response } from '@platformatic/php-node'
const response = new Response({
status: 500
})
console.log(response.status) // 500
- {Headers}
The HTTP headers included in the response.
import { Response } from '@platformatic/php-node'
const response = new Response({
headers: {
'Content-Type': ['application/json']
},
})
console.log(response.headers) // [Headers]
- {Buffer}
The HTTP response body.
import { Response } from '@platformatic/php-node'
const response = new Response({
body: Buffer.from(JSON.stringify({
error: 'bad stuff'
}))
})
console.log(response.body.toString()) // {"error":"bad stuff"}
- {Buffer}
Any logs captured during the request.
import { Response } from '@platformatic/php-node'
const response = new Response({
log: Buffer.from('some log message')
})
console.log(response.log.toString()) // some log message
- Returns: {Headers}
Construct a Headers object to manage HTTP headers. Note that this is currently only useful for reading from Request and Response types, not passing into them.
import { Headers } from '@platformatic/php-node'
const headers = new Headers()
name
{String} The header name for which to set a value.value
{String} The value to set for the named header.
This will set the value of the named header. If any prior headers have been set with this name they will be discarded.
import { Headers } from '@platformatic/php-node'
const headers = new Headers()
headers.set('Content-Type', 'application/json')
name
{String} The header name for which to add a value.value
{String} The value to add for the named header.
This will add to the associated values of the named header. If any prior headers have been set with this name they will also be kept.
import { Headers } from '@platformatic/php-node'
const headers = new Headers()
headers.add('Accept', 'application/json')
headers.add('Accept', 'text/html')
- Returns: {bool}
Checks if there are any values currently associated with the header name.
import { Headers } from '@platformatic/php-node'
const headers = new Headers()
headers.has('Content-Type') // false
- Returns: {string|undefined}
Retrieves the last value associated with the given header name.
import { Headers } from '@platformatic/php-node'
const headers = new Headers()
headers.add('Accept', 'application/json')
headers.add('Accept', 'text/html')
headers.get('Accept') // text/html
- Returns: {String[]}
Retrieves all values associated with the given header name.
import { Headers } from '@platformatic/php-node'
const headers = new Headers()
headers.add('Accept', 'application/json')
headers.add('Accept', 'text/html')
headers.getAll('Accept') // ['application/json', 'text/html']
- Returns: {String|undefined}
Merges all associated values into one header line. Note that his may be
incorrect for some header types which require separate header lines such as
the Set-Cookie
header.
import { Headers } from '@platformatic/php-node'
const headers = new Headers()
headers.add('Accept', 'application/json')
headers.add('Accept', 'text/html')
headers.getLine('Accept') // application/json, text/html
Delete all values associated with the given header name.
import { Headers } from '@platformatic/php-node'
const headers = new Headers()
headers.add('Accept', 'application/json')
headers.add('Accept', 'text/html')
headers.delete('Accept')
headers.get('Accept') // undefined
Remove all contained headers.
import { Headers } from '@platformatic/php-node'
const headers = new Headers()
headers.set('Content-Type', 'application/json')
headers.add('Accept', 'application/json')
headers.clear()
headers.get('Content-Type') // undefined
headers.get('Accept') // undefined
- {Number}
The number of header names present.
import { Headers } from '@platformatic/php-node'
const headers = new Headers()
headers.set('Content-Type', 'application/json')
headers.add('Accept', 'application/json')
headers.add('Accept', 'text/html')
headers.size // 3
- {Iterator}
Returns an iterator containing a (name, value)
tuple of header entries.
import { Headers } from '@platformatic/php-node'
const headers = new Headers()
headers.set('Content-Type', 'application/json')
headers.add('Accept', 'application/json')
headers.add('Accept', 'text/html')
for (const (name, value) of headers.entries()) {
// ('Content-Type', 'application/json')
// ('Accept', 'application/json')
// ('Accept', 'text/html')
}
- {Iterator}
Returns an iterator of header names.
import { Headers } from '@platformatic/php-node'
const headers = new Headers()
headers.set('Content-Type', 'application/json')
headers.add('Accept', 'application/json')
headers.add('Accept', 'text/html')
for (const name of headers.keys()) {
// 'Content-Type'
// 'Accept'
}
- {Iterator}
Returns an iterator of header values.
import { Headers } from '@platformatic/php-node'
const headers = new Headers()
headers.set('Content-Type', 'application/json')
headers.add('Accept', 'application/json')
headers.add('Accept', 'text/html')
for (const value of headers.values()) {
// 'application/json'
// 'application/json'
// 'text/html'
}
fn
{Function} Callback to call for each header entryvalue
{String} The value of the header entry.name
{String} The name of the header entry.headers
{Headers} The Header instance
Iterate over each header entry with a given callback.
import { Headers } from '@platformatic/php-node'
const headers = new Headers()
headers.set('Content-Type', 'application/json')
headers.add('Accept', 'application/json')
headers.add('Accept', 'text/html')
headers.forEach((value, name, headers) => {
// ('application/json', 'Content-Type', headers)
// ('application/json', 'Accept', headers)
// ('text/html', 'Accept', headers)
})
This project is part of the Platformatic ecosystem. Please refer to the main repository for contribution guidelines.
Apache-2.0