日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

快速开发一个PHP扩展

發布時間:2023/11/27 生活经验 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 快速开发一个PHP扩展 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
快速開發一個PHP擴展

作者:heiyeluren
時間:2008-12-5
博客:
http://blog.csdn.net/heiyeshuwu


本文通過非常快速的方式講解了如何制作一個PHP 5.2 環境的擴展(PHP Extension),希望能夠在圖文的方式下讓想快速學習的朋友了解一下制作過程。


需求:比如開發一個叫做 heiyeluren? 的擴展,擴展里就一個函數 heiyeluren_test(),輸入一個字符串,函數返回:Your input string: xxxxx。
要求:了解C/C++編程,熟悉PHP編程
環境:下載一份php對應版本的源碼,我這里是 php-5.2.6,先正常安裝php,假設我們的php安裝在 /usr/local/php 目錄,源碼在 /root/soft/php/php-5.2.6/,現在開始!


步驟一:生成擴展框架


cd /root/soft/php/php-5.2.6/ext
./ext_skel --extname=heiyeluren
cd /root/soft/php/php-5.2.6/ext/heiyeluren
vi config.m4
打開文件后去掉 dnl ,獲得下面的信息:
PHP_ARG_ENABLE(heiyeluren, whether to enable heiyeluren support,
[? --enable-heiyeluren?????????? Enable heiyeluren support])

保存退出.
(圖01)

?

?

第二步:編寫代碼

vi php_heiyeluren.h
找到:PHP_FUNCTION(confirm_heiyeluren_compiled); ,新增一行:
PHP_FUNCTION(heiyeluren_test);
保存退出。
(圖02)


vi heiyeluren.c
數組里增加我們的函數,找到 zend_function_entry heiyeluren_functions[],增加:
PHP_FE(heiyeluren, NULL)
(圖03)

?

再到 heiyeluren.c 文件最后面增加如下代碼:
PHP_FUNCTION(heiyeluren_test)
{
??? char *arg = NULL;
??? int arg_len, len;
??? char *strg;

??? if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &arg, &arg_len) == FAILURE) {
??????? return;
??? }

??? len = spprintf(&strg, 0, "Your input string: %s/n", arg);
??? RETURN_STRINGL(strg, len, 0);
}
保存退出。
(圖04)

?

?

?

第三步:編譯安裝

cd /root/soft/php/php-5.2.6/ext/heiyeluren
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config
make
make test
make install

現在看看是不是有個 /usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/heiyeluren.so
編輯php.ini,把擴展加入進去:
vi /usr/local/php/lib/php.ini
在[PHP]模塊下增加:
extension = heiyeluren.so
保存退出。
(圖05)

?

注意:如果你不存在擴展文件目錄,或者安裝報錯,那么可以自行建立這個目錄,然后把擴展拷貝到目錄下,然后記得把 php.ini 文件中的 extension_dir 修改為該目錄:
extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/"
(圖06)

?

?

第四步:檢查安裝結果
現在看看模塊加載了沒有:
/usr/local/php/bin/php -m,應該會打印出:
[PHP Modules]
...
heiyeluren
...
[Zend Modules]


然后重啟apache,輸出 phpinfo() ,應該能夠看到:
heiyeluren
heiyeluren support enabled

(圖07)

?

看看函數是否存在并且調用,在web目錄下建立:heiyeluren.php
<?php
echo "<pre>";
print_r(get_loaded_extensions());
print_r(get_extension_funcs('heiyeluren'));
echo heiyeluren_test('My first php extension');
echo "</pre>";
?>

訪問apache,應該能夠看到:
Array
(
??? ...
??? [33] => heiyeluren
)
Array
(
??? [0] => confirm_heiyeluren_compiled
??? [1] => heiyeluren_test
)
Your input string: heiyeluren
(圖08)


擴展制作成功!

?

?

更深入內容請參考這里:http://blog.csdn.net/taft/archive/2006/02/10/596291.aspx

總結

以上是生活随笔為你收集整理的快速开发一个PHP扩展的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。