Skip to content

Instantly share code, notes, and snippets.

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

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

Select an option

Save masakielastic/b3a0aa708a169dc7e52d819a35f6de8d to your computer and use it in GitHub Desktop.
ext-php-rs と Embed PHP を利用して PHP スクリプトを実行する

ext-php-rs で Embed PHP を利用する

sudo apt install libphp-embed

Cargo.toml

[package]
name = "php_embed_min"
version = "0.1.0"
edition = "2021"

[dependencies]
ext-php-rs = { version = "0.15", features = ["embed"] }

src/main.rs

use ext_php_rs::embed::Embed;

fn main() {
    let result = Embed::run(|| {
        // script.php の中の echo/print は(環境によっては)そのまま標準出力へ出ます
        Embed::run_script("script.php")?;
        Ok::<(), ext_php_rs::embed::EmbedError>(())
    });

    if let Err(e) = result {
        eprintln!("PHP script failed: {e:?}");
    }
}

script.php

<?php
echo "printing something...\n";
return 42;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment