加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

php - 扩展的编写和安装

(2013-08-30 15:20:35)
标签:

php

编写扩展

it

分类: php那些事

第一步:建立扩展骨架。


  1. cd  php-5.2.14/ext
  2. ./ext_skel --extname=laiwenhui

第二步:修改编译参数。


  1. cd php-5.2.14/ext/laiwenhui
  2. vi config.m4
  3. 去掉
  4. PHP_ARG_ENABLE(laiwenhui, whether to enable laiwenhui support,
  5. [  --enable-laiwenhui           Enable laiwenhui support])
  6. 两行前面的dnl
  7. 修改后为:
  8. dnl Otherwise use enable:

  9. PHP_ARG_ENABLE(laiwenhui, whether to enable laiwenhui support,
  10. dnl Make sure that the comment is aligned:
  11. [  --enable-laiwenhui           Enable laiwenhui support])

第三步:编写代码


  1. vim  php_laiwenhui.h


PHP_FUNCTION(confirm_laiwenhui_compiled);
后面新增一行
PHP_FUNCTION(test);
添加后为:
PHP_FUNCTION(confirm_laiwenhui_compiled);


  1. PHP_FUNCTION(test);

vim laiwenhui.c
在PHP_FE(confirm_laiwenhui_compiled, NULL)
后面添加
PHP_FE(test, NULL)
添加后为:
zend_function_entry laiwenhui_functions[] = {


  1. PHP_FE(confirm_laiwenhui_compiled,      NULL)          
  2. PHP_FE(test,    NULL)          
  3. {NULL, NULL, NULL}      
  4. **在文件最后面增加如下代码:**
  5. PHP_FUNCTION(test)
  6. {
  7. char *arg = "This is my first extention!";
  8. int  len;
  9. char *strg;

  10. len = spprintf(&strg, 0, "%s\n", arg);
  11. RETURN_STRINGL(strg, len, 0);
  12. }

第四步:编译代码
cd php-5.2.14/ext/laiwenhui


  1. /opt/module/php/bin/phpize
  2. ./configure --with-php-config=/opt/module/php/bin/php-config
  3. make
  4. make install

  5. 我的PHP安装路径为:/opt/module/php
  6. 这个时候会生成一个文件 /opt/module/php/lib/php/extensions/no-debug-non-zts-20060613/laiwenhui.so

  7. 编辑PHP配置文件php.ini,添加扩展:
  8. vim php.ini
  9. 在[PHP]模块下增加:
  10. extension = laiwenhui.so
  11. ;extension=php_zip.dll
  12. extension = laiwenhui.so

  13. php.ini 文件中的 extension_dir 修改为该目录:

extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/"


  1. ; Directory in which the loadable extensions (modules) reside.
  2. extension_dir = "/opt/module/php/lib/php/extensions/no-debug-non-zts-20060613/"

第五步:检查安装结果


  1. 1.重启apache或者php-fpm
  2. 2./opt/module/php/bin/php -m
  3. 看下是否有包含laiwenhui扩展。

第六步:执行测试代码

  

  1. 在网站根目录创建test.php
  2. vim test.php
  3. 代码内容如下

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有