Skip to content

PHP Library

Use MorphQL directly from PHP applications. The package ships with a bundled MorphQL engine — compatible with both Node.js and QuickJS for Node-less environments.

Installation

bash
composer require morphql/morphql

Packagist Version

Quick Start

php
<?php
require 'vendor/autoload.php';

use MorphQL\MorphQL;

$result = MorphQL::execute(
    'from json to json transform set greeting = "Hello, " + name',
    '{"name": "World"}'
);
// → '{"greeting":"Hello, World"}'

Usage

Static API

The execute() method supports both modern named parameters (PHP 8+) and a single-array calling convention for older PHP versions:

php
// PHP 8+ with named parameters
$result = MorphQL::execute(
    query: 'from json to json transform set x = a + b',
    data: '{"a": 1, "b": 2}'
);

// PHP 5.6-7.x — single options array
$result = MorphQL::execute(array(
    'query' => 'from json to json transform set x = a + b',
    'data'  => '{"a": 1, "b": 2}',
));

Reusable Instance

Create an instance with preset defaults when you need to run multiple transformations with the same configuration:

php
$morph = new MorphQL(array(
    'provider'   => 'server',
    'server_url' => 'http://localhost:3000',
    'api_key'    => 'my-secret',
));

$result = $morph->run('from json to xml', $data);
$other  = $morph->run('from csv to json', $csvData);

Providers

The library supports two execution providers:

ProviderBackendTransportRuntime
cli (default)Bundled engineproc_open()node or qjs
serverMorphQL REST servercURL / file_get_contents

The cli provider uses a bundled copy of the MorphQL engine. By default, it requires Node.js, but you can switch to the embedded QuickJS runtime for a completely self-contained, zero-config installation.

Node-less execution with QuickJS

QuickJS binaries are automatically downloaded for your platform during composer install or composer update. Just enable the runtime in your configuration:

php
$morph = new MorphQL([
    'runtime' => 'qjs'
]);

TIP

If you need to download binaries manually or for multiple platforms, you can run the included installer: php bin/install-qjs.php.

Configuration

Options are resolved in priority order: call params → constructor → env vars → defaults.

OptionEnv VarDefaultDescription
providerMORPHQL_PROVIDERclicli or server
runtimeMORPHQL_RUNTIMEnodenode or qjs
cli_pathMORPHQL_CLI_PATH(auto)Override CLI binary path
node_pathMORPHQL_NODE_PATHnodePath to Node.js binary
qjs_pathMORPHQL_QJS_PATH(auto)Path to QuickJS binary
cache_dirMORPHQL_CACHE_DIRSystem temp dirCLI query cache dir
server_urlMORPHQL_SERVER_URLlocalhost:3000Server base URL
api_keyMORPHQL_API_KEYAPI key for server auth
timeoutMORPHQL_TIMEOUT30Timeout in seconds

Error Handling

php
try {
    $result = MorphQL::execute('invalid query', '{}');
} catch (\RuntimeException $e) {
    echo 'Transform failed: ' . $e->getMessage();
} catch (\InvalidArgumentException $e) {
    echo 'Bad input: ' . $e->getMessage();
}

Compatibility

  • PHP 5.6+ — No type hints, array() syntax, compatible with legacy codebases
  • Node.js 18+ OR QuickJS — Required for the bundled CLI provider
  • Zero Composer dependencies — Only phpunit for development

Source

Released under the MIT License.