linux下 C语言对 php 扩展

一,搭建PHP环境

下载PHP 5.2.6 源码 并解压
编译安装,搭建PHP环境

二,创建扩展项目

进入源码目录
cd PHP5.2.6/ext/
创建名字为my_ext的项目,最终会生成my_ext.so三,更改配置和程序
$ vi ext/my_ext/config.m4根据你自己的选择将dnl PHP_ARG_WITH(my_ext,for my_ext support,
dnl Make sure that the comment is aligned:dnl [ --with-my_ext Include my_ext support])
修改PHP_ARG_WITH(my_ext,
Make sure that the comment is aligned:[ --with-my_ext Include my_ext support])
或者将dnl PHP_ARG_ENABLE(my_ext,whether to enable my_ext support,
dnl Make sure that the comment is aligned:dnl [ --enable-my_ext Enable my_ext support])
修改PHP_ARG_ENABLE(my_ext,Make sure that the comment is aligned:
[ --enable-my_ext Enable my_ext support])$ vi ext/my_ext/PHP_my_ext.h将
PHP_FUNCTION(confirm_my_ext_compiled); /* For testing,remove later. */
更改为
PHP_FUNCTION(say_hello);
$ vi ext/my_ext/my_ext.c将
zend_function_entry PHP5cpp_functions[] = {
PHP_FE(confirm_my_ext_compiled,NULL) /* For testing,remove later. */
{NULL,NULL,NULL} /* Must be the last line in PHP5cpp_functions[] */
};
更改为
zend_function_entry PHP5cpp_functions[] = {
PHP_FE(say_hello,NULL)
{NULL,NULL} /* Must be the last line in PHP5cpp_functions[] */
};在最后添加
PHP_FUNCTION(say_hello)
{
zend_printf("hello world\n");
}四,编译
$ cd my_ext
$ /usr/local/PHP/bin/PHPize
ps: 如果出现:Cannot find autoconf.……的错误信息,则需要安装 autoconf (安装过程略)
$ ./configure --with-PHP-config=/usr/local/PHP/bin/PHP-config
$ make这时会编译出 my_ext/modules/my_ext.so五,配置PHP.ini
将my_ext.so放入PHP/ext/目录$ vi PHP.ini修改添加如下:
extension_dir = '/usr/local/PHP/ext/'
.so 六,测试
$ vi test.PHP
PHP
say_hello();
?>$ /usr/local/PHP/bin/PHP test.PHP
hello world.则大功告成ps:如有问题请留言,大家共同探讨

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...