c擴展php的簡單介紹

本文目錄一覽:

如何編寫一個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.

搞定收工

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.

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/270555.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-12-16 13:37
下一篇 2024-12-16 13:37

相關推薦

  • PHP和Python哪個好找工作?

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

    編程 2025-04-29
  • Python簡單數學計算

    本文將從多個方面介紹Python的簡單數學計算,包括基礎運算符、函數、庫以及實際應用場景。 一、基礎運算符 Python提供了基礎的算術運算符,包括加(+)、減(-)、乘(*)、除…

    編程 2025-04-29
  • Python滿天星代碼:讓編程變得更加簡單

    本文將從多個方面詳細闡述Python滿天星代碼,為大家介紹它的優點以及如何在編程中使用。無論是剛剛接觸編程還是資深程序員,都能從中獲得一定的收穫。 一、簡介 Python滿天星代碼…

    編程 2025-04-29
  • Python海龜代碼簡單畫圖

    本文將介紹如何使用Python的海龜庫進行簡單畫圖,並提供相關示例代碼。 一、基礎用法 使用Python的海龜庫,我們可以控制一個小海龜在窗口中移動,並利用它的「畫筆」在窗口中繪製…

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

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

    編程 2025-04-29
  • Python櫻花樹代碼簡單

    本文將對Python櫻花樹代碼進行詳細的闡述和講解,幫助讀者更好地理解該代碼的實現方法。 一、簡介 櫻花樹是一種圖形效果,它的實現方法比較簡單。Python中可以通過turtle這…

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

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

    編程 2025-04-28
  • Python大神作品:讓編程變得更加簡單

    Python作為一種高級的解釋性編程語言,一直被廣泛地運用於各個領域,從Web開發、遊戲開發到人工智慧,Python都扮演著重要的角色。Python的代碼簡潔明了,易於閱讀和維護,…

    編程 2025-04-28
  • 用Python實現簡單爬蟲程序

    在當今時代,互聯網上的信息量是爆炸式增長的,其中很多信息可以被利用。對於數據分析、數據挖掘或者其他一些需要大量數據的任務,我們可以使用爬蟲技術從各個網站獲取需要的信息。而Pytho…

    編程 2025-04-28
  • 如何製作一個簡單的換裝遊戲

    本文將從以下幾個方面,為大家介紹如何製作一個簡單的換裝遊戲: 1. 遊戲需求和界面設計 2. 使用HTML、CSS和JavaScript開發遊戲 3. 實現遊戲的基本功能:拖拽交互…

    編程 2025-04-27

發表回復

登錄後才能評論