php版本必须>=8.0

文档地址 https://docs.rs/ext-php-rs/0.12.0/ext_php_rs/index.html

创建lib项目

cargo new ext_php_rs --lib
cd ext_php_rs 

编辑toml

[dependencies]
ext-php-rs = "0.12"
[lib]
crate-type = ["cdylib"]

编辑lib.rs 这里直接用的官网示例。

#![cfg_attr(windows, feature(abi_vectorcall))]

use ext_php_rs::prelude::*;

/// Gives you a nice greeting!
/// /// 
/// /// @param string $name Your name.
/// /// 
/// /// @return string Nice greeting!
#[php_function]
pub fn hello_world(name: String) -> String {
    format!("Hello, {}!", name)
}
///
///     // Required to register the extension with PHP.
#[php_module]
pub fn module(module: ModuleBuilder) -> ModuleBuilder {
      module
}

linux需要安装扩展

apt-get install libclang-dev

开始编译

cargo build --release

查看php的扩展目录

 php -i | grep extension_dir

复制so到扩展目录,我的是 /usr/lib/php/20230831

cp target/release/libext_php_rs.so /usr/lib/php/20230831

修改ini

extension=libphp-scrypt.so

测试

php -r "var_dump(hello_world("David"));" // string(13) "Hello, David!"

别人写的加密扩展项目,还是很简单的 https://github.com/appwrite/php-scrypt/tree/main


朝阳
1 声望0 粉丝