Maison  >  Article  >  développement back-end  >  PHP扩展开发01:第一个扩充【转】

PHP扩展开发01:第一个扩充【转】

WBOY
WBOYoriginal
2016-06-13 12:28:52974parcourir

PHP扩展开发01:第一个扩展【转】

 

我们先假设业务场景,是需要有这么一个扩展,提供一个叫ccvita_string的函数,他的主要作用是返回一段字符。(这个业务场景实在太假,大家就这么看看吧)对应的PHP代码可能是这样:

<span style="color: #0000ff;">function</span> ccvita_string(<span style="color: #800080;">$str</span><span style="color: #000000;">){     </span><span style="color: #800080;">$result</span> = '<a href="'.<span%20style=" color:>$str.'">Link</a>'<span style="color: #000000;">;     </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$result</span><span style="color: #000000;">;}</span>

第一步,生成代码
PHP为了扩展开发的方便,提供了一个类似代码生成器的工具ext_skel,具体可以参见说明。
首先我们创建一个文件ccvita.skel,它的内容为

<span style="color: #0000ff;">string</span> ccvita_string(<span style="color: #0000ff;">string</span> str)

 

就是告诉ext_skel这个东西,我们要做的扩展里面有个函数叫ccvita_string。然后执行

cd MooENV/src/php-<span style="color: #800080;">5.3</span>.<span style="color: #800080;">8</span>/ext/<span style="color: #000000;">.</span>/ext_skel --extname=ccvita --proto=<span style="color: #000000;">ccvita.skelcd ccvita</span>/

 

这时候,ccvita这个扩展的代码框架就已经出来了。

第二步,修改配置
然后修改config.m4文件将10、11、12三行最前面的dnl删除掉,就是将

dnl PHP_ARG_WITH(ccvita, for ccvita support,<span style="color: #000000;">dnl Make sure that the comment is aligned:dnl </span><span style="color: #800000; font-weight: bold;">[</span><span style="color: #800000;">  --with-ccvita             Include ccvita support</span><span style="color: #800000; font-weight: bold;">]</span>)

 

修改为

PHP_ARG_WITH(ccvita, for ccvita support,<span style="color: #000000;">Make sure that the comment is aligned:</span><span style="color: #800000; font-weight: bold;">[</span><span style="color: #800000;">  --with-ccvita             Include ccvita support</span><span style="color: #800000; font-weight: bold;">]</span>)

第三步,实现功能
修改源码ccvita.c文件
找到将ccvita_string这个函数修改为

<span style="color: #000000;">PHP_FUNCTION(ccvita_string){    </span><span style="color: #0000ff;">char</span> *str =<span style="color: #000000;"> NULL;    </span><span style="color: #0000ff;">int</span> argc =<span style="color: #000000;"> ZEND_NUM_ARGS();    </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> str_len;    </span><span style="color: #0000ff;">char</span> *<span style="color: #000000;">result;     </span><span style="color: #0000ff;">if</span> (zend_parse_parameters(argc TSRMLS_CC, <span style="color: #800000;">"</span><span style="color: #800000;">s</span><span style="color: #800000;">"</span>, &str, &str_len) ==<span style="color: #000000;"> FAILURE)         </span><span style="color: #0000ff;">return</span><span style="color: #000000;">;     str_len </span>= spprintf(&result, <span style="color: #800080;">0</span>, <span style="color: #800000;">"</span><span style="color: #800000;"><a href="%5C%22%.78s%5C%22">Link</a></span><span style="color: #800000;">"</span><span style="color: #000000;">, str);    RETURN_STRINGL(result, str_len, </span><span style="color: #800080;">0</span><span style="color: #000000;">); }</span>

 

第四步,编译扩展
保存后,开始编译

/usr/local/php/bin/<span style="color: #000000;">phpize.</span>/configure --with-php-config=/usr/local/php/bin/php-<span style="color: #000000;">config</span><span style="color: #0000ff;">make</span>

第五步,添加扩展
这时候,一切顺利的话,该扩展已经在modules/ccvita.so这个位置了。下面就是将这个扩展加入到PHP中去,让我们PHP程序可以调用到。

<span style="color: #0000ff;">cp</span> modules/ccvita.so /usr/local/php/ext/<span style="color: #000000;">vim </span>/usr/local/php/etc/<span style="color: #000000;">php.iniextension</span>=/usr/local/php/ext/<span style="color: #000000;">ccvita.so #在php.ini文件最后增加这一行service php</span>-<span style="color: #000000;">fpm restart #重启PHP服务</span><span style="color: #0000ff;">cp</span> ccvita.php /data/www/wwwroot/default/

相关代码,可以在我的github上clone出来,地址:https://github.com/KimiChen/PHP-EXT接下来就可以访问ccvita.php这个文件,测试扩展了。下一篇,将会涉及一些更深入的内容。

转自博客(http://www.ccvita.com/496.html)

Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn