用c写php扩展实属无奈,要用到16进制数据进行异或,偏移;php操作实在麻烦,不得已,决定自己写一个简单的动态库进行调用;
环境:ubuntu10.0.4
http://www.php.net/downloads.php
在写扩展之前需要编译源代码,不然后期要用的phpize无法找到;
具体:
sudo apt-get -y install libevent libevent-devel libxml2 libxml2-devel libmcrypt libmcrypt-devel libmcrypt libmcrypt-devel curl-devel libpng-devel libtool-ltdl-devel
用于编译php yum -y install gcc
1.tar zxvf php-5.3.10.tar.gz 2.cd php-5.3.10 3.配置 编译选项 ./configure --prefix=/usr/local/php \ --enable-fpm \ --with-fpm-user=fpm \ --with-fpm-group=fpm \ --with-mysql=mysqlnd \ --with-mysqli=mysqlnd \ --with-pdo-mysql=mysqlnd \ --without-pdo-sqlite \ --without-sqlite3 \ --without-sqlite \ --with-mysql-sock=/tmp/mysql.sock \ --with-curl \ --enable-mbstring \ --with-mhash \ --with-mcrypt \ --with-openssl \ --with-gd \ --enable-sockets \ --with-gettext \ --with-zlib \ --enable-zip \ --enable-soap \ --with-xmlrpc 不出意外,最后应该会看到以下结果: +--------------------------------------------------------------------+ | License: | | This software is subject to the PHP License, available in this | | distribution in the file LICENSE. By continuing this installation | | process, you are bound by the terms of this license agreement. | | If you do not agree with the terms of this license, you must abort | | the installation process at this point. | +--------------------------------------------------------------------+ Thank you for using PHP.
错误一:
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
//编译源码,漫长的一个过程,休息下先去喝杯茶再回来
成功后会看到:
Build complete. Don't forget to run 'make test'.
如果安装成功,就此php源码编译成功;
扩展方式有两种:
1.编译为.so库通过php.ini进行动态加载;
2.静态编译到php库中;
本次使用动态库方式;
创建的模块为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;
1.打开 php_socketCheck.h,在 PHP_FUNCTION(confirm_socketCheck_compiled); 之下加入函数声明:
PHP_FUNCTION(socketCheck);
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[] */ };
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); }
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