


As we all know, as a phper, I feel embarrassed about the limited functions of php, such as calling the ffmpeg video processing tool. There is no special extension to operate it, what? Using php system function call? For open source php scripts, this is too unsafe!
At this time, as a senior phper, you will consider developing a php extension and implement the operation of ffmpeg in the extension.
At present, there are almost no articles on how to develop PHP extensions in Rust on site c. Even the articles on the process of developing PHP extensions are very difficult to deal with. I am writing this article! ! !
Why use php extension?
Advantages:
1. The PHP extension is developed in C, so the speed is incredible.
2. High coupling, its appearance is used to enhance PHP.
3. High security. After all, the extension is a compiled program and the code is not open source.
Disadvantages:
1. It needs to be developed according to the PHP version and system environment, which is more troublesome. In other words, the 7.4 version of PHP, an extension developed in the Linux environment, only supports this PHP version and system.
2. You need to know C and C. Of course, this article is developed in rust. You need to understand the data types of C and be proficient in the operation and data type conversion of rust FFI.
3. Debugging is relatively troublesome.
Why use rust to develop php extensions?
The reason is very simple, and it also talks about the language features of rust.
1. Because of the "ownership" feature, your program is safer and will not have various "metaphysical bugs" like C.
2. It has the same performance as C.
3. After all, it is the most popular language, and I am very optimistic about its development.
rust development PHP extension process:
Of course, rust currently does not have a skeleton specifically for developing PHP extensions. So my logic is also very simple. I use rust to develop a static library and expose it to C [Involving knowledge of FFI]. We can directly introduce the rust static library into the official PHP skeleton and call its methods.
Development environment
Pagoda [CentOS 7.6], GCC [Involving the compilation of PHP extension skeleton, I have it built into the system here. If the compilation extension reports related errors, install it yourself], the corresponding PHP version source code, and the web environment [Install the corresponding PHP version, nginx, mysql, etc. in the pagoda]
Overall development process:
1. Prepare Pagoda
Pagoda installation process: CentOS Pagoda construction ( Super detailed)_One Code Superman's Blog-CSDN Blog_centos Pagoda
Here we take the development of php7.4 extension as an example.
2. Download the php7.4 liunx version source code
php official website: PHP: Hypertext Preprocessor
Notice! The source code version must be exactly the same as the PHP version of your environment! ! !
Download completed:
3. Upload the php source code Go to the pagoda
/usr/phper
Create a phper folder under usr, and then upload the source code compression package here.
Unzip the compressed package
## 4. Create an extension of our own
# Pay attention to setting the command line version, because the subsequent php commands must be of the same version!
在刚刚的目录下,点击终端,输入创建扩展命令。
php ext_skel.php --ext 扩展名称
这里就多出了一个新的扩展源码文件。
在该目录下点击终端,输入:
phpize
接着输入:
./configure --with-php-config=/www/server/php/74/bin/php-config
注意这个参数php路径,如果是别的版本,请自行在宝塔里安装找到对应版本路径,它们都是放一起的。
回车开始进行检查了
最后输入:
make
进行编译。
这个目录下便是编译出来的so扩展最终文件了!
让我们看下默认生成的扩展有哪些功能
查看主文件【需了解php扩展骨架,这里以它默认给的为例】
也就是说,刚刚编译出来的扩展,是有这两个函数的,咱们测试一下玩玩。
注意!每次修改主文件,都需要重新按上述命令跑一遍,否则不生效,很奇怪!
phpize ./configure --with-php-config=/www/server/php/74/bin/php-config make
5、使用扩展
复制刚刚生成的扩展文件到我们php环境的扩展里
配置php.ini加载hello.so扩展
extension = hello.so
保存后记得重新启动下php,否则不生效的!
在文件管理中点击终端,输入:
php -m
可以看到我们的扩展在列表中了。
创建一个站点,测试下扩展中两个函数。
看好,php版本是7.4
访问站点
没有问题哦!
当然也可以通过命令行运行php脚本查看结果【前提是网站那里php命令行版本设置的7.4】
php index.php
OK!从创建到生成到使用扩展的流程结束,接下来才进入正题,开始用rust开发扩展。
6、rust与php扩展的整合开发
开发工具:CLion
需要rust环境与CLion中rust插件的安装与配置,这个自行去百度,比我想象中的全!
创建一个hello命名的库项目
我们写两个导出函数,分别是加法功能和base64字符串解析功能。
lib.rs
#![crate_type = "staticlib"] extern crate libc; //使用C类型约束 use std::ffi::{CStr, CString}; use libc::{c_char, c_int}; //add_int【参数:两个c语言的int类型】:对两个int类型数值进行相加 #[no_mangle] pub extern "C" fn add_int(x:c_int, y:c_int) -> c_int{ //两个数相加 return x + y; } //base64_decode函数【参数:c语言的*char类型】:对字符串进行base64解码 #[no_mangle] pub extern "C" fn base64_decode(s:*const c_char) -> *mut c_char { //c char类型转&str let h = unsafe{CStr::from_ptr(s).to_str().unwrap()}; //base64 解码 let s = base64::decode(h.to_string()); if let Err(_s) = s { panic!("类型错误!"); } let n = String::from_utf8(s.unwrap().clone()).unwrap(); //String 转 C CString let a = CString::new(n.as_str()).unwrap(); //C CString 转 C char //这里实属无奈,因为rust ffi中阐述,对字符串返回只能是该字符串地址,所以需要该方法进行返回C才能接收到! let r = a.into_raw(); return r; }
Cargo.toml
[package] name = "hello" version = "0.1.0" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [lib] name = "hello" crate-type = ["staticlib"] [dependencies] libc = "*" base64 = "0.12.1"
注意在编译过程中涉及系统类型,不然在引入该静态库编译扩展可能报错,提示不支持。
编译64位静态库
rustup target add x86_64-unknown-linux-musl cargo build --target x86_64-unknown-linux-musl --release
编译32位静态库
rustup target add i686-unknown-linux-musl cargo build --target i686-unknown-linux-musl --release
这里我们是64位系统。
会生成一个.a文件,该文件便是liunx支持的静态库文件。
生成支持C语言的胶水头文件【用于C调用该库需要写的函数声明,很方便】
创建cbindgen.toml文件
内容:
language = "C"
安装cbindgen,创建头文件。
cargo install --force cbindgen cbindgen --config cbindgen.toml --crate 项目名称 --output 头文件名称.h
自动生成了C语言的函数声明hello.h文件,用于调用。
回到之前我们创建的hello扩展
创建lib文件夹
将刚刚编译出来的静态库.a文件上传到lib目录下
将刚刚创建的.h头文件上传到扩展目录下
配置.m4预编译文件【关键】
设置引入lib文件夹中的静态库文件
PHP_ADD_LIBRARY_WITH_PATH(hello, /usr/phper/php-7.4.30/ext/hello/lib, HELLO_SHARED_LIBADD) PHP_SUBST(HELLO_SHARED_LIBADD)
保存.m4
编写主文件
/* hello extension for PHP */ #ifdef HAVE_CONFIG_H # include "config.h" #endif #include "php.h" #include "ext/standard/info.h" #include "php_hello.h" #include "hello.h"//引入头文件 /* For compatibility with older PHP versions */ #ifndef ZEND_PARSE_PARAMETERS_NONE #define ZEND_PARSE_PARAMETERS_NONE() \ ZEND_PARSE_PARAMETERS_START(0, 0) \ ZEND_PARSE_PARAMETERS_END() #endif /* {{{ void hello_test1() */ PHP_FUNCTION(hello_test1) { ZEND_PARSE_PARAMETERS_NONE(); int num = add_int(1,2);//rust中两个数相加函数并返回。 php_printf("The extension %d is loaded and working!\r\n", num); } /* }}} */ /* {{{ string hello_test2( [ string $var ] ) */ PHP_FUNCTION(hello_test2) { char *var = "World"; size_t var_len = sizeof("World") - 1; zend_string *retval; ZEND_PARSE_PARAMETERS_START(0, 1) Z_PARAM_OPTIONAL Z_PARAM_STRING(var, var_len) ZEND_PARSE_PARAMETERS_END(); char *newstr = base64_decode(var);//rust中解析base64字符串并返回。 retval = strpprintf(0, "Hello %s", newstr); RETURN_STR(retval); } /* }}}*/ /* {{{ PHP_RINIT_FUNCTION */ PHP_RINIT_FUNCTION(hello) { #if defined(ZTS) && defined(COMPILE_DL_HELLO) ZEND_TSRMLS_CACHE_UPDATE(); #endif return SUCCESS; } /* }}} */ /* {{{ PHP_MINFO_FUNCTION */ PHP_MINFO_FUNCTION(hello) { php_info_print_table_start(); php_info_print_table_header(2, "hello support", "enabled"); php_info_print_table_end(); } /* }}} */ /* {{{ arginfo */ ZEND_BEGIN_ARG_INFO(arginfo_hello_test1, 0) ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO(arginfo_hello_test2, 0) ZEND_ARG_INFO(0, str) ZEND_END_ARG_INFO() /* }}} */ /* {{{ hello_functions[] */ static const zend_function_entry hello_functions[] = { PHP_FE(hello_test1, arginfo_hello_test1) PHP_FE(hello_test2, arginfo_hello_test2) PHP_FE_END }; /* }}} */ /* {{{ hello_module_entry */ zend_module_entry hello_module_entry = { STANDARD_MODULE_HEADER, "hello", /* Extension name */ hello_functions, /* zend_function_entry */ NULL, /* PHP_MINIT - Module initialization */ NULL, /* PHP_MSHUTDOWN - Module shutdown */ PHP_RINIT(hello), /* PHP_RINIT - Request initialization */ NULL, /* PHP_RSHUTDOWN - Request shutdown */ PHP_MINFO(hello), /* PHP_MINFO - Module info */ PHP_HELLO_VERSION, /* Version */ STANDARD_MODULE_PROPERTIES }; /* }}} */ #ifdef COMPILE_DL_HELLO # ifdef ZTS ZEND_TSRMLS_CACHE_DEFINE() # endif ZEND_GET_MODULE(hello) #endif
删除之前生成的扩展文件
重新生成扩展
phpize ./configure --with-php-config=/www/server/php/74/bin/php-config make
大小都变了,说明我们的静态库在里面了哈哈。
按上述使用扩展流程替换扩展
注意!替换扩展文件后要重启PHP哦,不然不生效!
7、测试rust开发的php扩展
网页测试
命令行测试
也可以通过php扩展骨架直接进行测试
编写要执行测试的扩展函数
--TEST-- hello_test2() Basic test --SKIPIF-- <?php if (!extension_loaded('hello')) { echo 'skip'; } ?> --FILE-- <?php hello_test1(); var_dump(hello_test2('5LiA56CB6LaF5Lq6')); ?> --EXPECT-- string(11) "Hello World" string(9) "Hello PHP"
扩展目录下直接输入:
make test
执行后 tests目录下输出了一个.out文件
是不是这样更方便了呢?
以上就是整体的开发流程,需要经通过的话还是多少要了解C语言、php扩展骨架、rust精通。
推荐学习:《PHP视频教程》
The above is the detailed content of An article explaining in detail how to develop PHP extensions in Rust (Liunx version). For more information, please follow other related articles on the PHP Chinese website!

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version
Visual web development tools

Notepad++7.3.1
Easy-to-use and free code editor

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft
