• 欢迎访问开心洋葱网站,在线教程,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站,欢迎加入开心洋葱 QQ群
  • 为方便开心洋葱网用户,开心洋葱官网已经开启复制功能!
  • 欢迎访问开心洋葱网站,手机也能访问哦~欢迎加入开心洋葱多维思维学习平台 QQ群
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏开心洋葱吧~~~~~~~~~~~~~!
  • 由于近期流量激增,小站的ECS没能经的起亲们的访问,本站依然没有盈利,如果各位看如果觉着文字不错,还请看官给小站打个赏~~~~~~~~~~~~~!

编写php扩展模块用c语言编写

OC/C/C++ 开心洋葱 2558次浏览 0个评论

编写前准备:

#添加组
groupadd www
#添加php-fpm用户
useradd -c php-fpm-user -g www -M php-fpm
# c和c++编译器
yum install -y gcc gcc-c++
# PHP扩展依赖
yum install -y libxml2-devel openssl-devel libcurl-devel libjpeg-devel libpng-devel libicu-devel openldap-devel

编译指令:

./configure --prefix=/data/soft/php\
 --with-libdir=lib64\
 --enable-fpm\
 --with-fpm-user=php-fpm\
 --with-fpm-group=www\
 --enable-mysqlnd\
 --with-mysql=mysqlnd\
 --with-mysqli=mysqlnd\
 --with-pdo-mysql=mysqlnd\
 --enable-opcache\
 --enable-pcntl\
 --enable-mbstring\
 --enable-soap\
 --enable-zip\
 --enable-calendar\
 --enable-bcmath\
 --enable-exif\
 --enable-ftp\
 --enable-intl\
 --with-openssl\
 --with-zlib\
 --with-curl\
 --with-gd\
 --with-zlib-dir=/usr/lib\
 --with-png-dir=/usr/lib\
 --with-jpeg-dir=/usr/lib\
 --with-gettext\
 --with-mhash\
 --with-ldap

编译后设置:

# 设置php-fpm开机自动启动
chmod +x /etc/init.d/php-fpm
chkconfig php-fpm on
cp /data/soft/php/etc/php-fpm.conf.default /data/soft/php/etc/php-fpm.conf
service php-fpm start

编写模块:
1.生成扩展模块
创建的模块为socketCheck 主要是为了给发送socket字符串时前3为加校验字符;

1.cd ~/php-5.3.15/ext/
2.执行./ext_skel –extname=socketCheck ,将在该目录生成 ~/php-5.3.15/ext/socketCheck
3.首先编辑 config.m4 文件,去掉第16行和第18行的注释(注释符号为 dnl 。)

16: PHP_ARG_ENABLE(hello, whether to enable hello support,
17: dnl Make sure that the comment is aligned:
18: [ –enable-hello Enable hello support])

然后执行 phpize 程序,生成configure脚本:

3.通过phpize生产configure编译工具

注意:/usr/local/php/bin/phpize 工具是通过前面编译php源代码生成,相对目录为编译源代码时设置的路径
步骤如下:

正常情况下

yang@ubuntu:~/php-5.3.15/ext/socketCheck$ /usr/local/php/bin/phpize
Configuring for:
PHP Api Version:         20090626
Zend Module Api No:      20090626
Zend Extension Api No:   220090626

错误:

yang@ubuntu:~/php-5.3.15/ext/socketCheck$ /usr/local/php/bin/phpize
Configuring for:
PHP Api Version:         20090626
Zend Module Api No:      20090626
Zend Extension Api No:   220090626
Cannot find autoconf. Please check your autoconf installation and the
$PHP_AUTOCONF environment variable. Then, rerun this script.

解决方法: sudo apt-get install autoconf 进行安装autoconf,安装完毕后再进行phpize

2.修改扩展模块
1.打开 php_socketCheck.h,在 PHP_FUNCTION(confirm_socketCheck_compiled); 之下加入函数声明:

PHP_FUNCTION(socketCheck);  

2.打开 socketCheck.c,在zend_function_entry socketCheck_functions[]加入以下内容。

zend_function_entry socketCheck_functions[] = {  
    PHP_FE(hello_add,   NULL)       /* For testing, remove later. */  
  
  
    PHP_FE_END    /* Must be the last line in socketCheck_functions[] */  
  
};  

3然后在 socketCheck.c 的最末尾书写socketCheck函数的内容(该函数就是我们需要的函数及具体实现)

 PHP_FUNCTION(socketCheck)  
    {  
      char *arg = NULL;  
    int arg_len, len;  
    char *strg;  
  
    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &arg, &arg_len) == FAILURE) {  
        return;  
    }  
  
    len = spprintf(&strg, 0, "Congratulations! You have successfully modified ext/%.78s/config.m4. Module %.78s is now compiled into PHP.", "socketCheck", arg);  
    RETURN_STRINGL(strg, len, 0);  
  
}  

3.编译扩展模块
1.配置

     ./configure --with-php --with-php-config=/usr/local/php/bin/php-config

2.编译

     make 

3.安装

     make install

4.扩展模块存放目录及配置生效

socketCheck.so 也已放到了php的扩展目录和(~/php-5.3.15/ext/socketCheck/modules)下

如果要是扩展模块可以调用,那么需要做两步操作:
1.配置文件添加扩展
打开php.ini 添加一行 extension=socketCheck.so
2.将socketCheck.so 拷贝到php默认的扩展目录,本机默认为(/usr/lib/php/modules)

3.重启apache

4.通过phpinfo()函数检查扩展是否加载成功;
或者
/usr/local/php/bin/php -m|grep socketCheck查看是否有 socketCheck.so


开心洋葱 , 版权所有丨如未注明 , 均为原创丨未经授权请勿修改 , 转载请注明编写php扩展模块用c语言编写
喜欢 (0)

您必须 登录 才能发表评论!

加载中……