c写php扩展之编译php源码,创建扩展模块,编写扩展demo

来源:互联网 发布:数据库建设规划 编辑:程序博客网 时间:2024/04/29 09:24

目录(?)[-]

  1. 下载php源代码
  2. 编译php源代码
    1. 安装编译php所需要的库文件
    2. 安装gcc
    3. cd 到php源码所在目录
    4. 具体编译
    5. 编译常见问题
    6. make
    7. make install
  3. 使用c程序添加php扩展
    1. 生成扩展模块
    2. 修改扩展模块
    3. 编译扩展模块

用c写php扩展实属无奈,要用到16进制数据进行异或,偏移;php操作实在麻烦,不得已,决定自己写一个简单的动态库进行调用;

环境:ubuntu10.0.4

1.下载php源代码

http://www.php.net/downloads.php


2.编译php源代码

在写扩展之前需要编译源代码,不然后期要用的phpize无法找到;

具体:

1.安装编译php所需要的库文件

sudo apt-get  -y install libevent libevent-devel libxml2 libxml2-devel libmcrypt libmcrypt-devel libmcrypt libmcrypt-devel curl-devel libpng-devel libtool-ltdl-devel

2.安装gcc

用于编译php yum -y install gcc

3.cd 到php源码所在目录 

4.具体编译

[html] view plaincopy
  1. 1.tar zxvf php-5.3.10.tar.gz  
  2.   
  3. 2.cd php-5.3.10  
  4.   
  5. 3.配置 编译选项  
  6.   
  7. ./configure --prefix=/usr/local/php \  
  8.  --enable-fpm \  
  9.  --with-fpm-user=fpm \  
  10.  --with-fpm-group=fpm \  
  11.  --with-mysql=mysqlnd \  
  12.  --with-mysqli=mysqlnd \  
  13.  --with-pdo-mysql=mysqlnd \  
  14.  --without-pdo-sqlite \  
  15.  --without-sqlite3 \  
  16.  --without-sqlite \  
  17.  --with-mysql-sock=/tmp/mysql.sock \  
  18.  --with-curl \  
  19.  --enable-mbstring \  
  20.  --with-mhash \  
  21.  --with-mcrypt \  
  22.  --with-openssl \  
  23.  --with-gd \  
  24.  --enable-sockets \  
  25.  --with-gettext \  
  26.  --with-zlib \  
  27.  --enable-zip \  
  28.  --enable-soap \  
  29.  --with-xmlrpc  
  30.   
  31. 不出意外,最后应该会看到以下结果:  
  32.   
  33. +--------------------------------------------------------------------+  
  34.  | License: |  
  35.  | This software is subject to the PHP License, available in this |  
  36.  | distribution in the file LICENSE. By continuing this installation |  
  37.  | process, you are bound by the terms of this license agreement. |  
  38.  | If you do not agree with the terms of this license, you must abort |  
  39.  | the installation process at this point. |  
  40.  +--------------------------------------------------------------------+  
  41.   
  42. Thank you for using PHP.  
  43.   
  44.    

编译常见问题

错误一:

configure: error: xml2-config not found. Please check your libxml2 installation.

而我已经安装过了libxml2,但是还是有这个提示:

解决办法:

# sudo apt-get install libxml2-dev

 

错误二:

configure: error: Please reinstall the BZip2 distribution

而我也已经安装了bzip2,网上找到得解决方案都是需要安装bzip2-dev,可是11.10里面没有这个库。

解决办法:在网上找到bzip2-1.0.5.tar.gz,解压,直接make ,sudo make install.(我使用的该源来自于http://ishare.iask.sina.com.cn/f/9769001.html)

 

错误三:

configure: error: Please reinstall the libcurl distribution -easy.h should be in <curl-dir>/include/curl/

解决办法:

# sudo apt-get install libcurl4-gnutls-dev

 

错误四:

configure: error: jpeglib.h not found.

解决办法:

# sudo apt-get install libjpeg-dev

 

错误五:

configure: error: png.h not found.

解决办法:

# sudo apt-get install libpng-dev

 

错误六:

configure: error: libXpm.(a|so) not found.

解决办法:

# sudo apt-get install libxpm-dev

 

错误七:

configure: error: freetype.h not found.

解决办法:

# sudo apt-get install libfreetype6-dev

 

错误八:

configure: error: Your t1lib distribution is not installed correctly. Please reinstall it.

解决办法:

# sudo apt-get install libt1-dev 

 

错误九:

configure: error: mcrypt.h not found. Please reinstall libmcrypt.

解决办法:

# sudo apt-get install libmcrypt-dev

 

错误十:

configure: error: Cannot find MySQL header files under yes.

Note that the MySQL client library is not bundled anymore!

解决办法:

# sudo apt-get install libmysql++-dev

 

错误十一:

configure: error: xslt-config not found. Please reinstall the libxslt >= 1.1.0 distribution

解决办法:

# sudo apt-get install libxslt1-dev

可见PHP源码安装之前需要先安装这些依赖,详细可见http://forum.ubuntu.org.cn/viewtopic.php?f=88&t=231159

如上错误都解决之后,再次./config....没有错误之后,

# make# sudo make install

5.make 

//编译源码,漫长的一个过程,休息下先去喝杯茶再回来

成功后会看到:
Build complete. Don't forget to run 'make test'.

6.make install

如果安装成功,就此php源码编译成功;


7.使用c程序添加php扩展

扩展方式有两种:

1.编译为.so库通过php.ini进行动态加载;

2.静态编译到php库中;

本次使用动态库方式;

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源代码生成,相对目录为编译源代码时设置的路径

步骤如下:

正常情况下

zhuse7en@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

错误:

zhuse7en@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); 之下加入函数声明:

[cpp] view plaincopy
  1. PHP_FUNCTION(socketCheck);  


2.打开 socketCheck.c,在zend_function_entry socketCheck_functions[]加入以下内容。
[cpp] view plaincopy
  1. zend_function_entry socketCheck_functions[] = {  
  2.     PHP_FE(hello_add,   NULL)       /* For testing, remove later. */  
  3.   
  4.   
  5.     PHP_FE_END    /* Must be the last line in socketCheck_functions[] */  
  6.   
  7. };  


 3然后在 socketCheck.c 的最末尾书写socketCheck函数的内容(该函数就是我们需要的函数及具体实现)
[cpp] view plaincopy
  1.     PHP_FUNCTION(socketCheck)  
  2.     {  
  3.       char *arg = NULL;  
  4.     int arg_len, len;  
  5.     char *strg;  
  6.   
  7.     if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &arg, &arg_len) == FAILURE) {  
  8.         return;  
  9.     }  
  10.   
  11.     len = spprintf(&strg, 0, "Congratulations! You have successfully modified ext/%.78s/config.m4. Module %.78s is now compiled into PHP.""socketCheck", arg);  
  12.     RETURN_STRINGL(strg, len, 0);  
  13.   
  14. }  

3.编译扩展模块

1.配置

./configure --with-php --with-php-config=/usr/local/php/bin/php-config
2.编译  make 3.安装 make install4.扩展模块存放目录及配置生效
socketCheck.so 也已放到了php的扩展目录和(~/php-5.3.15/ext/socketCheck/modules)下如果要是扩展模块可以调用,那么需要做两步操作:1.配置文件添加扩展
打开php.ini 添加一行 extension=socketCheck.so2.将socketCheck.so 拷贝到php默认的扩展目录,本机默认为(/usr/lib/php/modules)3.重启apache4.通过phpinfo()函数检查扩展是否加载成功;或者  
  /usr/local/php/bin/php -m|grep  socketCheck查看是否有 socketCheck.so 

0 0
原创粉丝点击