Hướng dẫn call rust from php - gọi gỉ từ php
[ -] [SRC] Thring Chất hàn−][src]Crate solderThư viện này cố gắng giúp bạn cải thiện ứng dụng PHP bằng cách sử dụng các tiện ích mở rộng được viết bằng gỉ. Ý tưởng là có thể viết mã hoàn toàn bằng gỉ, biên dịch nó bằng hàng hóa và tải thư viện trực tiếp vào PHP. Nó bắt đầu như một nĩa từ Lưu ý rằng đây là phiên bản đầu tiên của thùng này và rất nhiều thứ sẽ thay đổi. Ngoài ra, tôi không thể nhấn mạnh đủ mức độ không ổn định của cái thùng này ngay bây giờ. Sử dụng nó một cách thận trọng. Example: extern crate libc; extern crate solder; use solder::*; use solder::zend::*; use solder::info::*; #[no_mangle] pub extern fn php_module_info() { print_table_start(); print_table_row("A demo PHP extension written in Rust", "enabled"); print_table_end(); } #[no_mangle] pub extern fn get_module() -> *mut zend::Module { let function = FunctionBuilder::new(c_str!("hello_world"), hello_world) .with_arg(ArgInfo::new(c_str!("name"), 0, 0, 0)) .build(); ModuleBuilder::new(c_str!("hello_world"), c_str!("0.1.0-dev")) .with_info_function(php_module_info) .with_function(function) .build() .into_raw() } #[no_mangle] pub extern fn hello_world(_data: &ExecuteData, retval: &mut Zval) { let mut name_zval = Zval::new_as_null(); php_parse_parameters!(&mut name_zval); let name = String::try_from(name_zval).ok().unwrap(); let hello = format!("Hello {}", name); php_return!(retval, hello); } Để biên dịch nó, chúng ta cần thêm vào [build] rustflags = ["-C", "link-arg=-Wl,-undefined,dynamic_lookup"] Hơn, bạn biên dịch tiện ích mở rộng bằng $ cargo build && php -dextension=$(pwd)/target/debug/libhelloworld.so -a Compiling solder v0.1.0 (/src) Compiling helloworld v0.1.0 (/src/examples/helloworld) Finished dev [unoptimized + debuginfo] target(s) in 5.93s Interactive shell php > var_dump(hello_world("Bruno")); string(11) "Hello Bruno" php > Hiện tại, thùng này chỉ hoạt động với PHP7. Trong quá trình xây dựng, nó cố gắng lấy phiên bản API PHP và bản dựng mở rộng PHP từ PHP đã cài đặt. Nhưng, bạn có thể biên dịch cho các phiên bản khác bằng cách cài đặt thủ công PHP_API_Version và PHP_EXTENSINSION_BUILD Nếu bạn có câu hỏi hoặc ý tưởng cho dự án. Cứ tự nhiên liên lạc tôi.
|