用c寫php擴展,php擴展模塊開發

本文目錄一覽:

C擴展PHP 是什麼意思,能舉個簡單的例子嗎?

比如在win下的lamp環境吧,你想要php支持某個擴展不是需要把php.

ini文件

的那個注釋取消么,那個擴展是個文件類型為.dll的linux下為.soC語言能編譯成這兩種文件類型.或者說所有的擴展都是C編寫的.

如何用C語言編寫PHP擴展的詳解

1:預定義

在home目錄,也可以其他任意目錄,寫一個文件,例如caleng_module.def

內容是你希望定義的函數名以及參數:

int a(int x,int y)

string b(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

3:修改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])

4:修改caleng_module.c

代碼如下:

/* {{{ proto int a(int x, int y)

   */

PHP_FUNCTION(a)

{

int argc = ZEND_NUM_ARGS();

int x;

int y;

   int z;

if (zend_parse_parameters(argc TSRMLS_CC, “ll”, x, y) == FAILURE)

 return;

z=x+y;

 RETURN_LONG(z);

}

/* }}} */

/* {{{ proto string b(string str, int n)

   */

PHP_FUNCTION(b)

{

char *str = NULL;

   int argc = ZEND_NUM_ARGS();

   int str_len;

   long n;

   char *result;

   char *ptr;

   int result_length;

   if (zend_parse_parameters(argc TSRMLS_CC, “sl”, str, str_len, n) == FAILURE)

       return;

   result_length = str_len * n;

   result = (char *) emalloc(result_length + 1);

   ptr = result;

   while (n–) {

       memcpy(ptr, str, str_len);

       ptr += str_len;

   }

   *ptr = ‘\0’;

   RETURN_STRINGL(result, result_length, 0);

}

/* }}} */

5:生成擴展庫

#cd ./caleng_module

#/usr/local/php/bin/phpize

#./configure –with-php-config=/usr/local/php/bin/php-config

#make

#make install

6:到php的對應extensions目錄

如上圖所示

#cd /usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/

改目錄下有生成的caleng_module.so文件

7:修改php.ini

php.ini如果找不到可以從phpinfo()打出的信息看到

#cd /usr/local/php/lib/

php.ini增加擴展信息

extension=caleng_module.so

8:重啟Apache

# /usr/local/apache2/bin/apachectl restart

9:檢查加載

/usr/local/php/bin/php -m

10:PHP調用

代碼如下:

echo a(1,2);

輸出 3  就說明成功了!

下面是原文

Linux下用C開發PHP擴展

一、首先下載PHP源碼包,假設源碼包目錄為:/software/php-5.2.13

一、首先下載PHP源碼包,假設源碼包目錄為:/software/php-5.2.13

# cd /software/php-5.2.13/ext

二、假設我們要開發一個名為caleng_module的擴展,該擴展包含兩個函數:a–處理兩個整型相加和b-處理字符串重複輸出;

1、首先編寫一個函數定義文件,該文件編寫函數原型後綴為def,假設為:caleng_module.def

int a(int x, int y)

string b(string str, int n)

2、通過擴展骨架生成器,將在ext目錄下自動建立擴展目錄caleng_module

# ./ext_skel –extname=caleng_module –proto=caleng_module.def

3、修改配置文件: # vim /software/php-5.2.13/ext/caleng_module/config.m4,將如下行的注釋標籤”dnl”去掉,修改後如下所示:

PHP_ARG_ENABLE(myfunctions, whether to enable myfunctions support,

Make sure that the comment is aligned:

[  –enable-myfunctions           Enable myfunctions support])

4、完善函數a和b的功能: # vim /software/php-5.2.13/ext/caleng_module/caleng_module.c

PHP_FUNCTION(a)

{

   int x, y, z;

   int argc = ZEND_NUM_ARGS();

   if (zend_parse_parameters(argc TSRMLS_CC, “ll”, x, y) == FAILURE)

       return;

   z = x + y;

   RETURN_LONG(z);

}

PHP_FUNCTION(b)

{

   char *str = NULL;

   int argc = ZEND_NUM_ARGS();

   int str_len;

   long n;

   char *result;

   char *ptr;

   int result_length;

   if (zend_parse_parameters(argc TSRMLS_CC, “sl”, str, str_len, n) == FAILURE)

       return;

   result_length = str_len * n;

   result = (char *) emalloc(result_length + 1);

   ptr = result;

   while (n–) {

       memcpy(ptr, str, str_len);

       ptr += str_len;

   }

   *ptr = ‘\0’;

   RETURN_STRINGL(result, result_length, 0);

}

三、編譯安裝,假設php的安裝目錄為:/usr/localhost/webserver/php

# cd /software/php-5.2.13/ext/caleng_module

# /usr/localhost/webserver/php/bin/phpize

# ./configure –with-php-config=/usr/localhost/webserver/php/bin/php-config

# make

# make install

現在將在/usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20060613目錄下生成caleng_module.so文件

在php.ini配置文件中加入: extension=caleng_module.so.

C擴展PHP 是否可以達到加密代碼的目的

顯然可以,你用c擴展了你的功能,別人拿走你的代碼,沒有擴展,自然玩不轉。。這是最可靠的加密方法,雖然不是密文加密碼,但比密文可靠。。

如何編寫一個PHP的C擴展

一、首先下載PHP源碼包,假設源碼包目錄為:/software/php-5.2.13

一、首先下載PHP源碼包,假設源碼包目錄為:/software/php-5.2.13

# cd /software/php-5.2.13/ext

二、假設我們要開發一個名為caleng_module的擴展,該擴展包含兩個函數:a–處理兩個整型相加和b-處理字符串重複輸出;

1、首先編寫一個函數定義文件,該文件編寫函數原型後綴為def,假設為:caleng_module.def

int a(int x, int y)

string b(string str, int n)

2、通過擴展骨架生成器,將在ext目錄下自動建立擴展目錄caleng_module

# ./ext_skel –extname=caleng_module –proto=caleng_module.def

3、修改配置文件: # vim /software/php-5.2.13/ext/caleng_module/config.m4,將如下行的注釋標籤”dnl”去掉,修改後如下所示:

PHP_ARG_ENABLE(myfunctions, whether to enable myfunctions support,

Make sure that the comment is aligned:

[ –enable-myfunctions Enable myfunctions support])

4、完善函數a和b的功能: # vim /software/php-5.2.13/ext/caleng_module/caleng_module.c

PHP_FUNCTION(a)

{

int x, y, z;

int argc = ZEND_NUM_ARGS();

if (zend_parse_parameters(argc TSRMLS_CC, “ll”, x, y) == FAILURE)

return;

z = x + y;

RETURN_LONG(z);

}

PHP_FUNCTION(b)

{

char *str = NULL;

int argc = ZEND_NUM_ARGS();

int str_len;

long n;

char *result;

char *ptr;

int result_length;

if (zend_parse_parameters(argc TSRMLS_CC, “sl”, str, str_len, n) == FAILURE)

return;

result_length = str_len * n;

result = (char *) emalloc(result_length + 1);

ptr = result;

while (n–) {

memcpy(ptr, str, str_len);

ptr += str_len;

}

*ptr = ‘\0’;

RETURN_STRINGL(result, result_length, 0);

}

三、編譯安裝,假設php的安裝目錄為:/usr/localhost/webserver/php

# cd /software/php-5.2.13/ext/caleng_module

# /usr/localhost/webserver/php/bin/phpize

# ./configure –with-php-config=/usr/localhost/webserver/php/bin/php-config

# make

# make install

現在將在/usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20060613目錄下生成caleng_module.so文件

在php.ini配置文件中加入: extension=caleng_module.so.

搞定收工

nginx 下用c寫了一個php的擴展 但是出現了502錯誤

不是反不返回的問題,段錯誤是指你使用了非法的內存,一定是你 a() 函數里內存使用有問題,所以調用這個函數才會出錯,沒有代碼就無法解答了。C語言最常見的就是段錯誤了

原創文章,作者:EMLG,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/146011.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
EMLG的頭像EMLG
上一篇 2024-10-29 18:57
下一篇 2024-10-29 18:57

相關推薦

  • PHP和Python哪個好找工作?

    PHP和Python都是非常流行的編程語言,它們被廣泛應用於不同領域的開發中。但是,在考慮擇業方向的時候,很多人都會有一個問題:PHP和Python哪個好找工作?這篇文章將從多個方…

    編程 2025-04-29
  • 光模塊異常,SFP未認證(entityphysicalindex=6743835)——解決方案和

    如果您遇到類似optical module exception, sfp is not certified. (entityphysicalindex=6743835)的問題,那麼…

    編程 2025-04-29
  • PHP怎麼接幣

    想要在自己的網站或應用中接受比特幣等加密貨幣的支付,就需要對該加密貨幣擁有一定的了解,並使用對應的API進行開發。本文將從多個方面詳細闡述如何使用PHP接受加密貨幣的支付。 一、環…

    編程 2025-04-29
  • Python模塊下載與安裝指南

    如果想要擴展Python的功能,可以使用Python模塊來實現。但是,在使用之前,需要先下載並安裝對應的模塊。本文將從以下多個方面對Python模塊下載與安裝進行詳細的闡述,包括使…

    編程 2025-04-29
  • Python編程三劍客——模塊、包、庫

    本文主要介紹Python編程三劍客:模塊、包、庫的概念、特點、用法,以及在實際編程中的實際應用,旨在幫助讀者更好地理解和應用Python編程。 一、模塊 1、概念:Python模塊…

    編程 2025-04-29
  • Python如何下載第三方模塊

    想要使Python更加強大且具備跨平台性,我們可以下載許多第三方模塊。下面將從幾個方面詳細介紹如何下載第三方模塊。 一、使用pip下載第三方模塊 pip是Python的軟件包管理器…

    編程 2025-04-28
  • 如何使用pip安裝模塊

    pip作為Python默認的包管理系統,是安裝和管理Python包的一種方式,它可以輕鬆快捷地安裝、卸載和管理Python的擴展庫、模塊等。下面從幾個方面詳細介紹pip的使用方法。…

    編程 2025-04-28
  • Python datetime和time模塊用法介紹

    本文將詳細闡述Python datetime和time模塊的用法和應用場景,以幫助讀者更好地理解和運用這兩個模塊。 一、datetime模塊 datetime模塊提供了處理日期和時…

    編程 2025-04-28
  • Idea創建模塊時下面沒有啟動類的解決方法

    本文將從以下幾個方面對Idea創建模塊時下面沒有啟動類進行詳細闡述: 一、創建SpringBoot項目時沒有啟動類的解決方法 在使用Idea創建SpringBoot項目時,有可能會…

    編程 2025-04-28
  • 使用PHP foreach遍歷有相同屬性的值

    本篇文章將介紹如何使用PHP foreach遍歷具有相同屬性的值,並給出相應的代碼示例。 一、基礎概念 在講解如何使用PHP foreach遍歷有相同屬性的值之前,我們需要先了解幾…

    編程 2025-04-28

發表回復

登錄後才能評論