Linux下用C开发PHP扩展

一、首先下载PHP源码包,假设源码包目录为:/software/php-5.2.13

#> cd /software/php-5.2.13/ext

二、假设我们要开发一个名为caleng_module的扩展,该扩展包含两个函数:a--处理两个整型相加和b-处理字符串重复输出;

1、首先编写一个函数定义文件,该文件编写函数原型后缀为def,假设为:caleng_module.def

  1. int a(int x, int y)
  2. 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

  1. PHP_FUNCTION(a)
  2. {
  3. int x, y, z;
  4. int argc = ZEND_NUM_ARGS();
  5. if (zend_parse_parameters(argc TSRMLS_CC, "ll", &x, &y) == FAILURE)
  6. return;
  7. z = x + y;
  8. RETURN_LONG(z);
  9. }
  10. PHP_FUNCTION(b)
  11. {
  12. char *str = NULL;
  13. int argc = ZEND_NUM_ARGS();
  14. int str_len;
  15. long n;
  16. char *result;
  17. char *ptr;
  18. int result_length;
  19. if (zend_parse_parameters(argc TSRMLS_CC, "sl", &str, &str_len, &n) == FAILURE)
  20. return;
  21. result_length = str_len * n;
  22. result = (char *) emalloc(result_length + 1);
  23. ptr = result;
  24. while (n--) {
  25. memcpy(ptr, str, str_len);
  26. ptr += str_len;
  27. }
  28. *ptr = '/0';
  29. RETURN_STRINGL(result, result_length, 0);
  30. }

三、编译安装,假设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 扩展开发的更多相关文章

  1. iOS开发系列--App扩展开发

    概述 从iOS 8 开始Apple引入了扩展(Extension)用于增强系统应用服务和应用之间的交互.它的出现让自定义键盘.系统分享集成等这些依靠系统服务的开发变成了可能.WWDC 2016上众多更 ...

  2. PHP 扩展开发(将自己的一些代码封装成PHP扩展函数)

    今天时间不多,先给个地址,能搜到我这篇blog的朋友先看看我最近在看的一些文章.资料吧: 我的环境是 lnmp1.1 的 (LNMP一键安装包),所以要进行PHP扩展开发首先应该对环境配置和shell ...

  3. 关于PHP扩展开发(收藏)

    一.Linux shell命令: ls –lh    查看文件大小 du –a    查看文件及文件夹大小 -------------------------- nginx ------------- ...

  4. postgres扩展开发

    扩展开发的基本组成 demo--1.0.sql demo.c demo.control Makefile demo.c当中包含了自定义函数的实现,纯C语言,目录下可包含多个.c文件.demo-1.0. ...

  5. 【转发】NPAPI学习(Firefox和Chrome扩展开发 )

    NPAPI学习(Firefox和Chrome扩展开发 ) 2011-11-08 14:41:02 by [6yang], 1172 visits, 收藏 | 返回 Firefox和Chrome扩展开发 ...

  6. Chrome扩展开发之二——Chrome扩展中脚本的运行机制和通信方式

    目录: 0.Chrome扩展开发(Gmail附件管理助手)系列之〇——概述 1.Chrome扩展开发之一——Chrome扩展的文件结构 2.Chrome扩展开发之二——Chrome扩展中脚本的运行机制 ...

  7. PHP扩展开发相关总结

    1.线程安全宏定义 在TSRM/TSRM.h文件中有如下定义 #define TSRMLS_FETCH() void ***tsrm_ls = (void ***) ts_resource_ex(0, ...

  8. Firefox扩展开发

    Firefox扩展开发 (插件开发) Extension开发 入门教程 5步走 五步走   首先需要知道什么是"Firefox插件".这里说的"插件"只是一个通 ...

  9. Chrome浏览器扩展开发系列之十四

    Chrome浏览器扩展开发系列之十四:本地消息机制Native messaging 时间:2015-10-08 16:17:59      阅读:1361      评论:0      收藏:0    ...

  10. Chrome扩展开发(Gmail附件管理助手)系列之〇——概述

    目录: 0.Chrome扩展开发(Gmail附件管理助手)系列之〇——概述 1.Chrome扩展开发之一——Chrome扩展的文件结构 2.Chrome扩展开发之二——Chrome扩展中脚本的运行机制 ...

随机推荐

  1. Qt TabWidget QTabBar 宽高设置

    /*************************************************************************** * Qt TabWidget QTabBar ...

  2. 火狐下的GreaseMonkey和Chrome下的tampermonkey使用手记

    说明:GreaseMonkey的作用是让我们浏览器运行我们自己写的脚本,而且是在后台一直不断的运行,听着就让人兴奋. [ps:他们当然可以创建名单对哪些网站作用或者排除哪些站点]: 开始了:Greas ...

  3. 抛弃Https让Cas以Http协议提供单点登录服务

    本文环境: 1.apache-tomcat-7.0.50-windows-x86 2.cas-server-3.4.11 3.cas-client-3.2.1 将cas-server-webapp-3 ...

  4. 【spring源码学习】spring的远程调用实现源码分析

    [一]spring的远程调用提供的基础类 (1)org.springframework.remoting.support.RemotingSupport ===>spring提供实现的远程调用客 ...

  5. java多线程:synchronized和lock比较浅析

    转载:http://www.toutiao.com/a6392135944652587266/?tt_from=weixin&utm_campaign=client_share&app ...

  6. fn project 生产环境使用

    此为官方的参考说明   Running Fn in Production The QuickStart guide is intended to quickly get started and kic ...

  7. C#获取文件的MD5码

    using System; using System.IO; using System.Security.Cryptography; using System.Text; namespace Send ...

  8. Maven无法上传到到私服

    在pom.xml中插入如下代码: <distributionManagement> <repository> <id>nexus</id> <ur ...

  9. 对象的克隆,Dozer的使用

    现在有个复杂对象bean需要在赋值后在另一个地方用,想通过复制的方式拿到这个对象.首选是深度克隆,虽然发现该对象的父类已经实现了Cloneable接口,但父类是通过jar包引入的,而且在clone方法 ...

  10. 第13篇 PSR-2代码样式

    这个规范原文如下: 1. Overview Code MUST follow a "coding style guide" PSR [PSR-1]. Code MUST use 4 ...