用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.具体编译
编译常见问题
错误一:
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); 之下加入函数声明:
2.打开 socketCheck.c,在zend_function_entry socketCheck_functions[]加入以下内容。
3然后在 socketCheck.c 的最末尾书写socketCheck函数的内容(该函数就是我们需要的函数及具体实现)
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