Home > Backend Development > PHP Tutorial > An article explaining in detail how to develop PHP extensions in Rust (Liunx version)

An article explaining in detail how to develop PHP extensions in Rust (Liunx version)

青灯夜游
Release: 2023-04-11 08:24:02
forward
4730 people have browsed it

An article explaining in detail how to develop PHP extensions in Rust (Liunx version)

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
Copy after login

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

There is such a php file in the /usr/phper/php-7.4.30/ext directory, which can create extensions!

# Pay attention to setting the command line version, because the subsequent php commands must be of the same version!

 在刚刚的目录下,点击终端,输入创建扩展命令。

php ext_skel.php --ext 扩展名称
Copy after login

 

 这里就多出了一个新的扩展源码文件。

 

 在该目录下点击终端,输入:

phpize
Copy after login

 接着输入:

./configure --with-php-config=/www/server/php/74/bin/php-config
Copy after login

注意这个参数php路径,如果是别的版本,请自行在宝塔里安装找到对应版本路径,它们都是放一起的。

 回车开始进行检查了

 最后输入:

make
Copy after login

进行编译。

 

 这个目录下便是编译出来的so扩展最终文件了!

 让我们看下默认生成的扩展有哪些功能

 查看主文件【需了解php扩展骨架,这里以它默认给的为例】

 也就是说,刚刚编译出来的扩展,是有这两个函数的,咱们测试一下玩玩。

注意!每次修改主文件,都需要重新按上述命令跑一遍,否则不生效,很奇怪!

phpize
./configure --with-php-config=/www/server/php/74/bin/php-config
make
Copy after login
Copy after login

5、使用扩展

 复制刚刚生成的扩展文件到我们php环境的扩展里

 

 配置php.ini加载hello.so扩展

 

 

extension = hello.so
Copy after login

保存后记得重新启动下php,否则不生效的!

 

 

在文件管理中点击终端,输入:

php -m
Copy after login

可以看到我们的扩展在列表中了。

创建一个站点,测试下扩展中两个函数。

 看好,php版本是7.4

 

 访问站点

 没有问题哦!

当然也可以通过命令行运行php脚本查看结果【前提是网站那里php命令行版本设置的7.4】

php index.php
Copy after login

 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;
}
Copy after login

 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"
Copy after login

注意在编译过程中涉及系统类型,不然在引入该静态库编译扩展可能报错,提示不支持。

编译64位静态库

rustup target add x86_64-unknown-linux-musl
cargo build --target x86_64-unknown-linux-musl --release
Copy after login

编译32位静态库

rustup target add i686-unknown-linux-musl
cargo build --target i686-unknown-linux-musl --release
Copy after login

这里我们是64位系统。

 会生成一个.a文件,该文件便是liunx支持的静态库文件。

生成支持C语言的胶水头文件【用于C调用该库需要写的函数声明,很方便】

创建cbindgen.toml文件

内容:

language = "C"
Copy after login

 

 安装cbindgen,创建头文件。

cargo install --force cbindgen
cbindgen --config cbindgen.toml --crate 项目名称 --output 头文件名称.h
Copy after login

 

 自动生成了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)
Copy after login

 保存.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
Copy after login

删除之前生成的扩展文件

 重新生成扩展

 

phpize
./configure --with-php-config=/www/server/php/74/bin/php-config
make
Copy after login
Copy after login

 大小都变了,说明我们的静态库在里面了哈哈。

按上述使用扩展流程替换扩展

注意!替换扩展文件后要重启PHP哦,不然不生效!

7、测试rust开发的php扩展

 

 

 网页测试

 命令行测试

 也可以通过php扩展骨架直接进行测试

 

 

 编写要执行测试的扩展函数

--TEST--
hello_test2() Basic test
--SKIPIF--
<?php
if (!extension_loaded(&#39;hello&#39;)) {
	echo &#39;skip&#39;;
}
?>
--FILE--
<?php
hello_test1();
var_dump(hello_test2(&#39;5LiA56CB6LaF5Lq6&#39;));
?>
--EXPECT--
string(11) "Hello World"
string(9) "Hello PHP"
Copy after login

 扩展目录下直接输入:

make test
Copy after login

 

执行后 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!

Related labels:
php
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template