Skip to content

Instantly share code, notes, and snippets.

@masakielastic
Last active February 21, 2026 02:14
Show Gist options
  • Select an option

  • Save masakielastic/59f04bb82df3f952f485f1fccd2e52be to your computer and use it in GitHub Desktop.

Select an option

Save masakielastic/59f04bb82df3f952f485f1fccd2e52be to your computer and use it in GitHub Desktop.
ripht-php-sapi を使って Rust から PHP を実行する

ripht-php-sapi を使って Rust から PHP を実行する

php-src のビルド

mkdir -p ~/.ripht/src ~/.ripht/php
cd ~/.ripht/src


git clone https://github.com/php/php-src.git
cd php-src

./buildconf --force

./configure \
  --prefix="$HOME/.ripht/php" \
  --enable-embed=static \
  --disable-zts

make -j"$(nproc)"
make install

構成

Cargo.toml

[package]
name = "ripht-php-sapi"
version = "0.1.0"
edition = "2021"

[dependencies]
anyhow = "1.0.102"
ripht-php-sapi = "0.1.0-rc.3"

build.rs

fn main() {
    println!("cargo:rustc-link-lib=xml2");
    println!("cargo:rustc-link-lib=sqlite3");
}

src/main.rs

use ripht_php_sapi::prelude::*;
use ripht_php_sapi::CliRequest;
use std::path::Path;

fn main() -> anyhow::Result<()> {
    let sapi = RiphtSapi::instance();

    let script = Path::new("hello.php");

    let ctx = CliRequest::new()
        .build(script)?; // <- ExecutionContext

    let result = sapi.execute(ctx)?;

    println!("Status: {}", result.status_code());
    println!("Body:\n{}", result.body_string());

    Ok(())
}

hello.php

<?php
echo "Hello from PHP via Rust!\n";

実行

RIPHT_PHP_SAPI_PREFIX="$HOME/.ripht/php" cargo build
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment