c写成php的扩展_用C语言编写PHP扩展
1:預定義
vi myfunctions.def
string self_concat(string str, int n)
2:到PHP源碼目錄的ext目錄
#cd /usr/local/php-5.4.0/ext/
執行命令,生成對應擴展目錄
#./ext_skel --extname=caleng_module --proto=/home/hm/caleng_module.def
./ext_skel --extname=myfunctions --proto=myfunctions.def
3:修改config.m4
vi config.m4
去掉dnl的注釋
PHP_ARG_ENABLE(caleng_module, whether to enable caleng_module support,
Make sure that the comment is aligned:
[? --enable-caleng_module? ? ? ? ? Enable caleng_module support])
php源碼根目錄
./buildconf
修改vi myfunctions.c
PHP_FUNCTION(self_concat)
{
char *str = NULL;
int argc = ZEND_NUM_ARGS();
int str_len;
long n;
char *result; /* Points to resulting string */
char *ptr; /* Points at the next location we want to copy to */
int result_length; /* Length of resulting string */
if (zend_parse_parameters(argc TSRMLS_CC, "sl", &str, &str_len, &n) == FAILURE)
return;
/* Calculate length of result */
result_length = (str_len * n);
/* Allocate memory for result */
result = (char *) emalloc(result_length + 1);
/* Point at the beginning of the result */
ptr = result;
while (n--) {
/* Copy str to the result */
memcpy(ptr, str, str_len);
/* Increment ptr to point at the next position we want to write to */
ptr += str_len;
}
/* Null terminate the result. Always null-terminate your strings
even if they are binary strings */
*ptr = '\0';
/* Return result to the scripting engine without duplicating it*/
RETURN_STRINGL(result, result_length, 0);
}
執行
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config
make
make install
修改
vi php.ini
php.ini增加擴展信息
extension=myfunctions.so
重啟Nginx和php-fpm。
檢查有沒有加載都這個模塊了
/usr/local/php/bin/php -m 或者 phpinfo()
把PHP加入到環境變量? export PATH=$PATH:/usr/local/webserver/php/bin
執行
php -r "echo self_concat('One', 3);"
參考:
http://www.cnblogs.com/benio/archive/2010/09/25/1834655.html
http://www.laruence.com/2008/08/16/301.html
http://blog.csdn.net/sanbingyutuoniao123/article/details/51921510
總結
以上是生活随笔為你收集整理的c写成php的扩展_用C语言编写PHP扩展的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: php unicode 插入 mysql
- 下一篇: bean validation校验方法参