Use C language to develop php extension under linux system

1. Generate the extension file directory

First, we need to download the source code of php:  https://github.com/php/php-src/releases

Select the corresponding version and download the compressed package.

The default version of Pagoda panel php7.3 is php7.3.3, so take php7.3.3 as an example.

The real download address is as follows:

https://codeload.github.com/php/php-src/zip/refs/heads/PHP-7.3.3

After downloading, upload it to the linux server and unzip it.

You can also use the linux command wget to download, and then decompress it on the server.

wget https://github.com/php/php-src/archive/php-7.3.3.tar.gz
tar -zvxf php-7.3.3.tar.gz

After the decompression is complete, enter the php directory ext/

cd php-src-php-7.3.3/ext

Then run the ext_skel.php file in the ext folder.

php ext_skel.php --ext myext

The execution results are as follows:

[root@localhost ext]# php ext_skel.php --ext myext
Copying config scripts... done
Copying sources... done
Copying tests... done

Success. The extension is now ready to be compiled into PHP. To do so, use the
following steps:

cd /path/to/php-src
./buildconf
./configure --enable-myext
make

Don't forget to run tests once the compilation is done:
make test TESTS=ext/myext/tests

Thank you for using PHP!

At this time, all the extension-related files have been generated. You only need to modify the code and then compile it. Don’t modify the code for now, and compile it first to see the effect.

2. Compile and install

Go to the extension folder

cd myext

There are the following files in it:

tests  //文件夹,用于扩展安装之后的测试
config.m4  //配置编译的执行命令
config.w32  //win32编译
php_myext.h //扩展的头文件  
myext.c //扩展c文件

We directly generate the ./configure file through phpize: (in the case of multiple versions, you need to pay attention to the phpize version)

phpize

Then install the extension as normal:

./configure --with-php-config=/www/server/php/73/bin/php-config
make
make install

After the compilation is complete, add a line of configuration to php.ini:

Note the reference to the real path of the extension

extension = myext.so

3. Test

Enter the tests folder and run the test file:

cd tests
php 001.phpt 
php 002.phpt 
php 003.phpt

You can see the output of the custom extension myext (I have multiple versions of php, so I use the php73 version to run)

[root@localhost tests]# php73 001.phpt 
--TEST--
Check if myext is loaded
--SKIPIF--
--FILE--
The extension "myext" is available--EXPECT--
The extension "myext" is available
[root@localhost tests]# php73 002.phpt 
--TEST--
myext_test1() Basic test
--SKIPIF--
--FILE--
The extension myext is loaded and working!
NULL
--EXPECT--
The extension myext is loaded and working!
NULL
[root@localhost tests]# php73 003.phpt 
--TEST--
myext_test2() Basic test
--SKIPIF--
--FILE--
string(11) "Hello World"
string(9) "Hello PHP"
--EXPECT--
string(11) "Hello World"
string(9) "Hello PHP"

 At this point, the php extension has been compiled and installed successfully.

You can also edit the PHP file to call the test function, and the effect will be more intuitive.

<?php
$test1 = myext_test1();
$test2 = myext_test2();
var_dump($test1);
var_dump($test2);
?>

4. Custom extension function

If you want to add new functions, you only need to modify the myext.c file.

Add a function hello with parameters (you can follow the generated function PHP_FUNCTION(myext_test2)):


PHP_FUNCTION(hello)
{
   char *var = "World";//定义一个world字符串变量
   size_t var_len = sizeof("World") - 1;//定义长度
   zend_string *retval;//定义zend_string类型的变量

   ZEND_PARSE_PARAMETERS_START(0, 1)//设置参数数量限制,前面的代表着最少传0个参数,后面的代表了最多传1个
      Z_PARAM_OPTIONAL //可选参数 ,不强制传参
      Z_PARAM_STRING(var, var_len)//如果有传值,则把值赋值给字符串变量var
   ZEND_PARSE_PARAMETERS_END();//设置参数结束

   retval = strpprintf(0, "Hello %s", var);//格式化字符串

   RETURN_STR(retval);//返回值
}

Add function to register module


static const zend_function_entry myext_functions[] = {
   PHP_FE(myext_test1,    arginfo_myext_test1)
   PHP_FE(myext_test2,    arginfo_myext_test2)
   PHP_FE(hello,    NULL)
   PHP_FE_END
};

 It can be used after recompiling and installing the extension after modification.

Guess you like

Origin blog.csdn.net/weixin_58881595/article/details/125115244