如何用C语言编写PHP扩展的详解


Posted in PHP onJune 13, 2013

1:预定义
在home目录,也可以其他任意目录,写一个文件,例如caleng_module.def
内容是你希望定义的函数名以及参数:
int a(int x,int y)
string b(string str,int n)

2:到php源码目录的ext目录
#cd /usr/local/php-5.4.0/ext/

执行命令,生成对应扩展目录
#./ext_skel --extname=caleng_module --proto=/home/hm/caleng_module.def

3:修改config.m4
去掉dnl的注释

PHP_ARG_ENABLE(caleng_module, whether to enable caleng_module support,
Make sure that the comment is aligned:
[  --enable-caleng_module           Enable caleng_module support])

4:修改caleng_module.c

/* {{{ proto int a(int x, int y)
    */
PHP_FUNCTION(a)
{
 int argc = ZEND_NUM_ARGS();
 int x;
 int y;
    int z;
 if (zend_parse_parameters(argc TSRMLS_CC, "ll", &x, &y) == FAILURE)   return;
z=x+y;
  RETURN_LONG(z);
}
/* }}} */
/* {{{ proto string b(string str, int n)
    */
PHP_FUNCTION(b)
{
 char *str = NULL;
    int argc = ZEND_NUM_ARGS();
    int str_len;
    long n;
    char *result;
    char *ptr;
    int result_length;
    if (zend_parse_parameters(argc TSRMLS_CC, "sl", &str, &str_len, &n) == FAILURE)
        return;
    result_length = str_len * n;
    result = (char *) emalloc(result_length + 1);
    ptr = result;
    while (n--) {
        memcpy(ptr, str, str_len);
        ptr += str_len;
    }
    *ptr = '\0';
    RETURN_STRINGL(result, result_length, 0);
}
/* }}} */

5:生成扩展库
#cd ./caleng_module
#/usr/local/php/bin/phpize
#./configure --with-php-config=/usr/local/php/bin/php-config
#make
#make install

如何用C语言编写PHP扩展的详解

6:到php的对应extensions目录
如上图所示
#cd /usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/
改目录下有生成的caleng_module.so文件

7:修改php.ini
php.ini如果找不到可以从phpinfo()打出的信息看到
#cd /usr/local/php/lib/
php.ini增加扩展信息
extension=caleng_module.so
 
8:重启Apache
# /usr/local/apache2/bin/apachectl restart

9:检查加载
 /usr/local/php/bin/php -m

10:PHP调用

echo a(1,2);

输出 3  就说明成功了!
=================================
下面是原文
Linux下用C开发PHP扩展
一、首先下载PHP源码包,假设源码包目录为:/software/php-5.2.13
一、首先下载PHP源码包,假设源码包目录为:/software/php-5.2.13
#> cd /software/php-5.2.13/ext
二、假设我们要开发一个名为caleng_module的扩展,该扩展包含两个函数:a--处理两个整型相加和b-处理字符串重复输出;
1、首先编写一个函数定义文件,该文件编写函数原型后缀为def,假设为:caleng_module.def
int a(int x, int y)
string b(string str, int n)
2、通过扩展骨架生成器,将在ext目录下自动建立扩展目录caleng_module
#> ./ext_skel --extname=caleng_module --proto=caleng_module.def
3、修改配置文件: #> vim /software/php-5.2.13/ext/caleng_module/config.m4,将如下行的注释标签"dnl"去掉,修改后如下所示:
PHP_ARG_ENABLE(myfunctions, whether to enable myfunctions support,
Make sure that the comment is aligned:
[  --enable-myfunctions           Enable myfunctions support])
4、完善函数a和b的功能: #> vim /software/php-5.2.13/ext/caleng_module/caleng_module.c
PHP_FUNCTION(a)
{
    int x, y, z;

    int argc = ZEND_NUM_ARGS();

    if (zend_parse_parameters(argc TSRMLS_CC, "ll", &x, &y) == FAILURE)
        return;
    z = x + y;
    RETURN_LONG(z);
}
PHP_FUNCTION(b)
{
    char *str = NULL;
    int argc = ZEND_NUM_ARGS();
    int str_len;
    long n;
    char *result;
    char *ptr;
    int result_length;

    if (zend_parse_parameters(argc TSRMLS_CC, "sl", &str, &str_len, &n) == FAILURE)
        return;
    result_length = str_len * n;
    result = (char *) emalloc(result_length + 1);
    ptr = result;
    while (n--) {
        memcpy(ptr, str, str_len);
        ptr += str_len;
    }
    *ptr = '\0';
    RETURN_STRINGL(result, result_length, 0);
}
三、编译安装,假设php的安装目录为:/usr/localhost/webserver/php
#> cd /software/php-5.2.13/ext/caleng_module
#> /usr/localhost/webserver/php/bin/phpize
#> ./configure --with-php-config=/usr/localhost/webserver/php/bin/php-config
#> make
#> make install
现在将在/usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20060613目录下生成caleng_module.so文件
在php.ini配置文件中加入: extension=caleng_module.so.
搞定收工

PHP 相关文章推荐
php5.4以下版本json不支持不转义内容中文的解决方法
Jan 13 PHP
Laravel 中获取上一篇和下一篇数据
Jul 27 PHP
PHP表单提交后引号前自动加反斜杠的原因及三种办法关闭php魔术引号
Sep 30 PHP
PHP模板引擎Smarty内置变量调解器用法详解
Apr 11 PHP
Yii数据读取与跳转参数传递用法实例分析
Jul 12 PHP
微信自定义菜单的创建/查询/取消php示例代码
Aug 05 PHP
yii2多图上传组件的使用教程
May 10 PHP
php curl优化下载微信头像的方法总结
Sep 07 PHP
修改Laravel自带的认证系统的User类的命名空间的步骤
Oct 15 PHP
laravel ORM关联关系中的 with和whereHas用法
Oct 16 PHP
PHP超全局变量实现原理及代码解析
Sep 01 PHP
laravel添加角色和模糊搜索功能的实现代码
Jun 22 PHP
探讨:如何编写PHP扩展
Jun 13 #PHP
PHP APC的安装与使用详解
Jun 13 #PHP
eAccelerator的安装与使用详解
Jun 13 #PHP
探讨Hessian在PHP中的使用分析
Jun 13 #PHP
深入PHP变量存储的详解
Jun 13 #PHP
深入PHP中的HashTable结构详解
Jun 13 #PHP
基于PHP输出缓存(output_buffering)的深入理解
Jun 13 #PHP
You might like
默默简单的写了一个模板引擎
2007/01/02 PHP
php自动加载autoload机制示例分享
2014/02/20 PHP
获取DOM对象的几种扩展及简写
2006/10/09 Javascript
扩展jquery实现客户端表格的分页、排序功能代码
2011/03/16 Javascript
lyhucSelect基于Jquery的Select数据联动插件
2011/03/29 Javascript
jQuery切换网页皮肤并保存到Cookie示例代码
2014/06/16 Javascript
一个不错的字符串转码解码函数(自写)
2014/07/31 Javascript
EasyUI实现二级页面的内容勾选的方法
2015/03/01 Javascript
Nodejs的express使用教程
2015/11/23 NodeJs
关于JS中setTimeout()无法调用带参函数问题的解决方法
2016/06/21 Javascript
jQuery图片前后对比插件beforeAfter用法示例【附demo源码下载】
2016/09/20 Javascript
JS实现图片旋转动画效果封装与使用示例
2018/07/09 Javascript
详解ES6 Symbol 的用途
2018/10/14 Javascript
layui checkbox默认选中,获取选中值,清空所有选中项的例子
2019/09/02 Javascript
bootstrap实现嵌套模态框的实例代码
2020/01/10 Javascript
微信小程序实现下拉加载更多商品
2020/12/29 Javascript
python实现的简单抽奖系统实例
2015/05/22 Python
Python实现两个list对应元素相减操作示例
2017/06/09 Python
python cs架构实现简单文件传输
2020/03/20 Python
Django 查询数据库并返回页面的例子
2019/08/12 Python
python获取响应某个字段值的3种实现方法
2020/04/30 Python
python 决策树算法的实现
2020/10/09 Python
python中类与对象之间的关系详解
2020/12/16 Python
HTML5 CSS3实现一个精美VCD包装盒个性幻灯片案例
2014/06/16 HTML / CSS
使用HTML5中的contentEditable来将多行文本自动增高
2016/03/01 HTML / CSS
html5实现滑块功能之type="range"属性
2020/02/18 HTML / CSS
美国维生素、补充剂、保健食品购物网站:Vitacost
2016/08/05 全球购物
哥德堡通行证:Gothenburg Pass
2019/12/09 全球购物
美国精品地毯网站:Boutique Rugs
2020/03/04 全球购物
全球采购的街头服饰和帽子:Urban Excess
2020/10/28 全球购物
工作的心得体会
2013/12/31 职场文书
物流专业专科生职业生涯规划书
2014/09/14 职场文书
贷款收入证明范本
2015/06/12 职场文书
MySQL 存储过程的优缺点分析
2021/05/20 MySQL
Redis性能监控的实现
2021/07/09 Redis
Python机器学习实战之k-近邻算法的实现
2021/11/27 Python