开发的第一个PHP扩展
下载php源码php-5.4.23.tar.gz,解压,进入/home/hubo/php-5.4.23/ext/扩展目录 wget http://cn2.php.net/get/php-5.4.23.tar.gz/from/this/mirror tar -xzvf php-5.4.23.tar.gz cd php-5.4.23/ext/ 在ext目录中新建config.m4文件 PHP_ARG_ENABLE(heiyoubo,
下载php源码php-5.4.23.tar.gz,解压,进入/home/hubo/php-5.4.23/ext/扩展目录
wget http://cn2.php.net/get/php-5.4.23.tar.gz/from/this/mirror
tar -xzvf php-5.4.23.tar.gz
cd php-5.4.23/ext/
在ext目录中新建config.m4文件
<span>PHP_ARG_ENABLE(heiyoubo, [Whether to enable the </span><span>"</span><span>heiyoubo</span><span>"</span><span> extension], [ enable</span>-heiyoubo Enable <span>"</span><span>heiyoubo</span><span>"</span><span> extension support]) </span><span>if</span> test $PHP_HEIYOUBO != <span>"</span><span>no</span><span>"</span>; <span>then</span><span> PHP_SUBST(HEIYOUBO_SHARED_LIBADD) PHP_NEW_EXTENSION(heiyoubo, heiyoubo.c, $ext_shared) </span><span>fi</span>
在ext目录中新建heiyoubo.c文件
<span>#ifdef HAVE_CONFIG_H #include </span><span>"</span><span>config.h</span><span>"</span> <span>#endif</span> <span>//</span><span>加载php头文件</span> #include <span>"</span><span>php.h</span><span>"</span> <span>#define</span> phpext_heiyoubo_ptr &heiyoubo_module_entry<span> ZEND_FUNCTION(heiyoubo_hello) { php_printf(</span><span>"</span><span>Hello World Heiyoubo!\n</span><span>"</span><span>); } </span><span>static</span> zend_function_entry heiyoubo_functions[] =<span> { ZEND_FE(heiyoubo_hello, NULL) { NULL, NULL, NULL } }; </span><span>//</span><span>module entry</span> zend_module_entry heiyoubo_module_entry =<span> { </span><span>#if</span> ZEND_MODULE_API_NO >= 20010901<span> STANDARD_MODULE_HEADER, </span><span>#endif</span> <span>"</span><span>heiyoubo</span><span>"</span>, <span>//</span><span>这个地方是扩展名称,往往我们会在这个地方使用一个宏。</span> heiyoubo_functions, <span>/*</span><span> Functions </span><span>*/</span><span> NULL, </span><span>/*</span><span> MINIT </span><span>*/</span><span> NULL, </span><span>/*</span><span> MSHUTDOWN </span><span>*/</span><span> NULL, </span><span>/*</span><span> RINIT </span><span>*/</span><span> NULL, </span><span>/*</span><span> RSHUTDOWN </span><span>*/</span><span> NULL, </span><span>/*</span><span> MINFO </span><span>*/</span> <span>#if</span> ZEND_MODULE_API_NO >= 20010901 <span>"</span><span>2.1</span><span>"</span>, <span>//</span><span>这个地方是我们扩展的版本</span> <span>#endif</span><span> STANDARD_MODULE_PROPERTIES }; #ifdef COMPILE_DL_HEIYOUBO ZEND_GET_MODULE(heiyoubo) </span><span>#endif</span>
运行phpize,准备 PHP 扩展库的编译环境
[hubo@test15169x ~/php-5.4.23/ext]$ /usr/local/php/bin/phpize
Configuring for:
PHP Api Version: 20100412
Zend Module Api No: 20100525
Zend Extension Api No: 220100525
configure的时候要开启heiyoubo扩展,并且指定php-config的目录,获取所安装的 PHP 配置的信息
[hubo@test15169x ~/php-5.4.23/ext]$ ./configure --enable-heiyoubo --with-php-config=/usr/local/php/bin/php-config
[hubo@test15169x ~/php-5.4.23/ext]$ make
[hubo@test15169x ~/php-5.4.23/ext]$ make test
heiyoubo.so扩展已经生成到module目录
[hubo@test15169x ~/php-5.4.23/ext]$ ll modules/*
-rw-rw-r-- 1 hubo hubo 799 01-09 16:53 modules/heiyoubo.la
-rwxrwxr-x 1 hubo hubo 26K 01-09 16:53 modules/heiyoubo.so
将heiyoubo.so文件拷贝到php的扩展目录
[hubo@test15169x ~/php-5.4.23/ext]$ php -ini | grep extension_dir
extension_dir => /usr/local/php/lib/php/extensions/no-debug-non-zts-20100525 => /usr/local/php/lib/php/extensions/no-debug-non-zts-20100525
[hubo@test15169x ~/php-5.4.23/ext]$ cp modules/heiyoubo.so /usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/
将heiyoubo.so加到php的扩展文件/usr/local/etc/cgi/php.ini中 extension = "heiyoubo.so"
[hubo@test15169x ~/php-5.4.23/ext]$ php --ini
Configuration File (php.ini) Path: /usr/local/etc/cgi
Loaded Configuration File: /usr/local/etc/cgi/php.ini
[hubo@test15169x ~/php-5.4.23/ext]$ php -r 'var_dump(get_loaded_extensions());' | grep heiyoubo
string(8) "heiyoubo"
验证安装成功。执行C扩展中函数heiyoubo_hello();执行成功
[hubo@test15169x ~/php-5.4.23/ext]$ php -r 'heiyoubo_hello();'
Hello World Heiyoubo!
参考链接:
http://www.php.net/manual/zh/internals2.buildsys.configunix.php
https://github.com/walu/phpbook/blob/master/5.1.md 《Extending and Embedding PHP》中文版翻译 PHP扩展开发及内核应用
http://www.laruence.com/2009/04/28/719.html
http://www.php-internals.com/

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

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

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

How To Set Up Visual Studio Code (VS Code) for PHP Development
