ripht-php-sapi
Safe, pragmatic Rust bindings for embedding PHP via the embed SAPI.
Why ripht-php-sapi?
-
Provide tooling that will allow additional PHP tooling to be built in Rust.
-
I hadn't seen another Rust crate offering comparable features.
-
I'm planning to build more tooling on this (stay tuned...).
Requirements
This crate requires PHP built with the embed SAPI as a static library:
&&
Set RIPHT_PHP_SAPI_PREFIX to your PHP installation root containing:
lib/libphp.a(PHP embed SAPI)include/php/(PHP headers)
Or install to one of the default fallback locations: ~/.ripht/php, ~/.local/php, or /usr/local.
Important Notes: This crate is focuses on the non-ZTS build of PHP. There aren't currently plans to support ZTS builds.
Tip: Tools like Static PHP CLI can simplify building PHP with the embed SAPI. See CONTRIBUTING.md for development setup options.
Quick start
Add the crate to your Cargo.toml:
[]
= "0.1.0-rc.5"
Example usage:
For convenience, the crate provides a prelude module — import it with use ripht_php_sapi::prelude::*; to get commonly used types.
Web Example
Simulate an HTTP request. This populates $_GET, $_SERVER, etc.
use *;
let sapi = instance;
let script = new;
let req = get
.with_query_param
.with_header
.build
.expect;
let res = sapi.execute.expect;
assert_eq!;
println!;
PUT Method Example
Use WebRequest::new() with any HTTP method. The Method enum implements TryFrom<&str>, making it easy to parse methods from incoming requests:
use *;
let sapi = instance;
let script = new;
let method_str = "pUt";
let method = try_from.expect;
let req = new
.with_uri
.with_content_type
.with_body
.with_header
.build
.expect;
let res = sapi.execute.expect;
println!;
println!;
CLI Example
Run a script as if from the command line. This sets argc/argv and avoids HTTP superglobals.
use *;
let sapi = instance;
let script = new;
let req = new
.with_arg
.with_env
.build
.expect;
let res = sapi.execute.expect;
println!;
You only write safe Rust and don't have to worry about the low-level SAPI details.
Here's a minimal example that uses a single hook callback to stream output as it arrives:
use ;
;
sapi.execute_with_hooks.expect;
Development notes
- The build script expects a PHP build root that contains
lib/libphp.a(static embed SAPI) and headers. SetRIPHT_PHP_SAPI_PREFIXto point at your PHP build prefix if necessary. - Example debug/run helpers and bench configuration are in
.cargo/config.toml.example.
Examples
The crate includes comprehensive examples demonstrating various use cases:
basic_execution.rs- Simple GET request handlingenv_and_ini.rs- Environment variables and INI overridespost_form.rs- Form data handlingpost_json.rs- JSON request processingfile_upload.rs- File upload handlinghooks_basic.rs- Basic hook implementationhooks_comprehensive.rs- Full hook lifecyclehooks_output_handling.rs- Output processinghooks_streaming_callback.rs- StreamingCallback helperstreaming_output.rs- Output streamingsession_handling.rs- PHP sessionshttp_server.rs- HTTP server integrationtracing_demo.rs- Observability integrationerror_handling.rs- Error managementexception_recovery.rs- Exception handlingmemory_pressure.rs- Memory usage testingfile_io.rs- File system operationsencoding_gaunlet.rs- Character encoding tests
Run any example with:
Benchmarking
Performance benchmarks are available in the benches/ directory:
sapi_comparison.rs- Compare against php-fpm and FrankenPHPthroughput.rs- Request throughput testing
Standard Benchmarks
Run benchmarks with:
# Basic benchmarks
# External server comparison (requires setup)
BENCH_COMPARE=1 \
BENCH_FPM_BIN=/path/to/php-fpm \
BENCH_FRANKENPHP_BIN=/path/to/frankenphp \
For benchmark configuration and debug helpers, see .cargo/config.toml.example.
Support Development
This project is part of a larger educational initiative about PHP internals and Rust FFI.
Contributing
If you'd like to help, open an issue or a PR — small, focused changes are appreciated.
License
MIT