PHP 使用c语言开发自定义PHP扩展

从github上下载源码

 wget https://github.com/php/php-src/archive/refs/heads/master.zip

解压源码压缩包

unzip master.zip

进入PHP源码目录

#cd php-src-master/ext
#ll

在这里插入图片描述

执行php ./ext_skel.php --ext hello,创建自定义扩展hello配置文件目录

#php ./ext_skel.php --ext hello
Copying config scripts... done
Copying sources... done
Copying tests... done

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

cd /data/php-src-master/ext/hello
phpize
./configure
make

Don't forget to run tests once the compilation is done:
make test

Thank you for using PHP!

在这里插入图片描述

进入hello扩展文件目录

#cd /data/php-src-master/ext/hello

#ll
total 620
drwxr-xr-x  5 root root   4096 Jun 18 15:23 ./
drwxr-xr-x 72 root root   4096 Jun 18 15:22 ../
-rw-r--r--  1 root root    500 Jun 18 15:22 .gitignore
drwxr-xr-x  2 root root   4096 Jun 18 15:23 autom4te.cache/
drwxr-xr-x  2 root root   4096 Jun 18 15:23 build/
-rw-r--r--  1 root root   1786 Jun 18 15:23 config.h.in
-rw-r--r--  1 root root   3231 Jun 18 15:22 config.m4
-rw-r--r--  1 root root    204 Jun 18 15:22 config.w32
-rwxr-xr-x  1 root root 427366 Jun 18 15:23 configure*
-rw-r--r--  1 root root   5431 Jun 18 15:23 configure.ac
-rw-r--r--  1 root root   1856 Jun 18 15:22 hello.c
-rw-r--r--  1 root root    133 Jun 18 15:22 hello.stub.php
-rw-r--r--  1 root root    556 Jun 18 15:22 hello_arginfo.h
-rw-r--r--  1 root root    309 Jun 18 15:22 php_hello.h
-rw-r--r--  1 root root 139754 Jun 18 15:23 run-tests.php
drwxr-xr-x  2 root root   4096 Jun 18 15:22 tests/

在这里插入图片描述

# cat php_hello.h 

在这里插入图片描述

这边自定义了test1(),test2()2个扩展函数

# cat hello.c 

在这里插入图片描述
在这里插入图片描述

// 扩展的模块信息
zend_module_entry hello_module_entry = {
STANDARD_MODULE_HEADER,
“hello”, /* 扩展名称 /
ext_functions, /
函数列表 /
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 的 phpinfo() 函数输出有关扩展模块的信息,例如版本号、作者、功能列表等 /
PHP_HELLO_VERSION, /
Version 扩展版本*/
STANDARD_MODULE_PROPERTIES
};

执行phpize、./configure、make、make test命令,对扩展进行编译安装(如果需要调整hello扩展内容,则自行修改hello.c、php_hello.h等相关文件)

phpize
./configure
make

make test

在这里插入图片描述

ll命令查看目录文件

# ll
total 1500
drwxr-xr-x  8 root root   4096 Jun 18 15:27 ./
drwxr-xr-x 72 root root   4096 Jun 18 15:22 ../
-rw-r--r--  1 root root    500 Jun 18 15:22 .gitignore
drwxr-xr-x  2 root root   4096 Jun 18 15:26 .libs/
-rw-r--r--  1 root root   8829 Jun 18 15:26 Makefile
-rw-r--r--  1 root root      0 Jun 18 15:26 Makefile.fragments
-rw-r--r--  1 root root    693 Jun 18 15:26 Makefile.objects
drwxr-xr-x  2 root root   4096 Jun 18 15:26 autom4te.cache/
drwxr-xr-x  2 root root   4096 Jun 18 15:23 build/
-rw-r--r--  1 root root   1930 Jun 18 15:26 config.h
-rw-r--r--  1 root root   1822 Jun 18 15:26 config.h.in
-rw-r--r--  1 root root  21333 Jun 18 15:26 config.log
-rw-r--r--  1 root root   3231 Jun 18 15:22 config.m4
-rwxr-xr-x  1 root root     58 Jun 18 15:26 config.nice*
-rwxr-xr-x  1 root root  45294 Jun 18 15:26 config.status*
-rw-r--r--  1 root root    204 Jun 18 15:22 config.w32
-rwxr-xr-x  1 root root 427366 Jun 18 15:26 configure*
-rw-r--r--  1 root root   5431 Jun 18 15:26 configure.ac
-rwxr-xr-x  1 root root 427366 Jun 18 15:23 configure~*
-rw-r--r--  1 root root   1856 Jun 18 15:22 hello.c
-rw-r--r--  1 root root   4444 Jun 18 15:26 hello.dep
-rw-r--r--  1 root root    931 Jun 18 15:26 hello.la
-rw-r--r--  1 root root    284 Jun 18 15:26 hello.lo
-rw-r--r--  1 root root    133 Jun 18 15:22 hello.stub.php
-rw-r--r--  1 root root    556 Jun 18 15:22 hello_arginfo.h
drwxr-xr-x  2 root root   4096 Jun 18 15:26 include/
-rwxr-xr-x  1 root root 342337 Jun 18 15:26 libtool*
drwxr-xr-x  2 root root   4096 Jun 18 15:26 modules/
-rw-r--r--  1 root root    309 Jun 18 15:22 php_hello.h
-rw-r--r--  1 root root 139754 Jun 18 15:26 run-tests.php
drwxr-xr-x  2 root root   4096 Jun 18 15:26 tests/

执行make install

#make install
Installing shared extensions:     /usr/lib/php/20210902/

ll命令查看so扩展目录

#ll /usr/lib/php/20210902/
total 9636
drwxr-xr-x 3 root root    4096 Jun 18 15:31 ./
drwxr-xr-x 4 root root    4096 Mar 12 10:57 ../
drwxr-xr-x 2 root root    4096 Mar 12 11:38 build/
-rw-r--r-- 1 root root   35080 Aug 18  2023 calendar.so
-rw-r--r-- 1 root root   14600 Aug 18  2023 ctype.so
-rw-r--r-- 1 root root  190728 Aug 18  2023 dom.so
-rw-r--r-- 1 root root   96520 Aug 18  2023 exif.so
-rw-r--r-- 1 root root  174344 Aug 18  2023 ffi.so
-rw-r--r-- 1 root root 7153984 Aug 18  2023 fileinfo.so
-rw-r--r-- 1 root root   67848 Aug 18  2023 ftp.so
-rw-r--r-- 1 root root   18696 Aug 18  2023 gettext.so
-rwxr-xr-x 1 root root   38320 Jun 18 15:31 hello.so*
-rw-r--r-- 1 root root   51464 Aug 18  2023 iconv.so
-rw-r--r-- 1 root root 1006632 Aug 18  2023 opcache.so
-rw-r--r-- 1 root root  121096 Aug 18  2023 pdo.so
-rw-r--r-- 1 root root  284936 Aug 18  2023 phar.so
-rw-r--r-- 1 root root   43272 Aug 18  2023 posix.so
-rw-r--r-- 1 root root   39176 Aug 18  2023 readline.so
-rw-r--r-- 1 root root   18696 Aug 18  2023 shmop.so
-rw-r--r-- 1 root root   59656 Aug 18  2023 simplexml.so
-rw-r--r-- 1 root root  104712 Aug 18  2023 sockets.so
-rw-r--r-- 1 root root   22792 Aug 18  2023 sysvmsg.so
-rw-r--r-- 1 root root   14600 Aug 18  2023 sysvsem.so
-rw-r--r-- 1 root root   22792 Aug 18  2023 sysvshm.so
-rw-r--r-- 1 root root   35080 Aug 18  2023 tokenizer.so
-rw-r--r-- 1 root root   59656 Aug 18  2023 xml.so
-rw-r--r-- 1 root root   43272 Aug 18  2023 xmlreader.so
-rw-r--r-- 1 root root   51464 Aug 18  2023 xmlwriter.so
-rw-r--r-- 1 root root   39176 Aug 18  2023 xsl.so

在这里插入图片描述

执行php -I | grep hello 命令查看hello扩展

# php -i | grep hello
PWD => /data/php-src-master/ext/hello
$_SERVER['PWD'] => /data/php-src-master/ext/hello

在这里插入图片描述

查看ext扩展ini文件位置

# php -i | grep ext 
/etc/php/8.1/cli/conf.d/20-gettext.ini,
default_mimetype => text/html => text/html
docref_ext => no value => no value
extension_dir => /usr/lib/php/20210902 => /usr/lib/php/20210902
mail.force_extra_parameters => no value => no value
gettext
GetText Support => enabled
bzip2 compression => disabled (install ext/bz2)
PWD => /data/php-src-master/ext/hello
OLDPWD => /data/php-src-master/ext
$_SERVER['PWD'] => /data/php-src-master/ext/hello

在这里插入图片描述

编辑文件ini文件,增加hello.so扩展

 vi /etc/php/8.1/cli/conf.d/20-gettext.ini

在这里插入图片描述

再次执行php -i | grep hello,查看hello扩展是否安装成功

#php -i | grep hello
hello
hello support => enabled
PWD => /data/php-src-master/ext/hello
$_SERVER['PWD'] => /data/php-src-master/ext/hello

在这里插入图片描述

可以看到hello扩展已经添加成功,到此完成PHP自定义扩展(hello)的配置及安装

最后

重启php-fpm,测试hello扩展是否哪个正常使用

测试test1(),test2() 是否正常执行

# php -r 'echo test1();echo PHP_EOL;'
The extension hello is loaded and working!

在这里插入图片描述

# php -r 'echo test2();echo PHP_EOL;'
Hello World

在这里插入图片描述

hello扩展目录

在这里插入图片描述

  • 17
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值