linux下 C语言对 php 扩展

yipeiwu_com5年前PHP代码库
一,搭建php环境
下载php 5.2.6 源码 并解压
编译安装,搭建php环境

二,创建扩展项目

进入源码目录
cd php5.2.6/ext/
./ext_skel --extname=my_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, for my_ext support,
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, whether to enable my_ext support,

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, 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放入/usr/local/php/ext/目录

$ vi php.ini

修改添加如下:
extension_dir = '/usr/local/php/ext/'
extension=my_ext.so  

六,测试
$ vi test.php
<?php
   say_hello();
?>

$ /usr/local/php/bin/php test.php
hello world.

则大功告成

ps:如有问题请留言,大家共同探讨

相关文章

PHP iconv()函数字符编码转换的问题讲解

在php中iconv函数库能够完成各种字符集间的转换,是php编程中不可缺少的基础函数库;但有时候iconv对于部分数据转码会无缘无故的少一些。比如在转换字符"—"到gb2312时会出错...

PHP上传图片时判断上传文件是否为可用图片的方法

本文实例讲述了PHP上传图片时判断上传文件是否为可用图片的方法。分享给大家供大家参考,具体如下: 这里利用getimagesize函数: function isImage($file...

php弹出提示框的是实例写法

php中弹出对话框的方法如下,一般,在提交信息后需要弹出对话框提示,之后可以自动关闭对话框,弹出对话框有下面集中方法,其实都是利用javascript中的alert()方法。提示结束后关...

PHP接口多继承及tarits实现多继承效果的方法

本文实例讲述了PHP接口多继承及tarits实现多继承效果的方法。分享给大家供大家参考,具体如下: 接口多继承 在PHP的面向对象中,接口可以继承接口。PHP类只能继承一个父类(单继承)...

PHP实现批量修改文件名的方法示例

本文实例讲述了PHP实现批量修改文件名的方法。分享给大家供大家参考,具体如下: 需求描述: 某个文件夹下有100个文件,现在需要将这个100个文件的文件名后添加字符串Abc(后缀名保持不...