Custom PHP Extension in C

For Linux (Ubuntu Server)


1. Installation

[codesyntax lang=”bash” lines=”no” container=”pre” tab_width=”4″]

$ sudo apt-get install gcc
$ sudo apt-get install php5-dev

[/codesyntax]


2. PHP Source Package

[codesyntax lang=”bash” lines=”no” container=”pre” tab_width=”4″]

$ su
$ cd /home/kim
$ wget http://cn.php.net/get/php-5.3.2.tar.gz/from/this/mirror
$ mv mirror php-5.3.2.tar.gz
$ tar -xzf php-5.3.2.tar.gz

[/codesyntax]


3. Create Extension

[codesyntax lang=”bash” lines=”no” container=”pre” tab_width=”4″]

$ cd php-5.3.2/ext
$ ./ext_skel --extname=ttt
$ cd ttt/
$ vi config.m4

[/codesyntax]

PHP_ARG_WITH(example, for example support,

[ –with-example Include example support])

[codesyntax lang=”bash” lines=”no” container=”pre” tab_width=”4″]

$ phpize
$ ./configure
$ make
$ sudo make install

[/codesyntax]


4. Config and Restart Apache

[codesyntax lang=”bash” lines=”no” container=”pre” tab_width=”4″]

$ vi /etc/php5/apache2/conf.d/ttt.ini

[/codesyntax]

extension=ttt.so

[codesyntax lang=”bash” lines=”no” container=”pre” tab_width=”4″]

$ apache2ctl restart

[/codesyntax]


5. For PHP-FPM

[codesyntax lang=”bash” lines=”no” container=”pre” tab_width=”4″]

$ vi /etc/php5/fpm/conf.d/ttt.ini

[/codesyntax]

extension=ttt.so

[codesyntax lang=”bash” lines=”no” container=”pre” tab_width=”4″]

$ /etc/init.d/php5-fpm restart

[/codesyntax]


6. Extension Functions

[codesyntax lang=”bash” lines=”no” container=”pre” tab_width=”4″]

$ cd php-5.3.2/ext/ttt
$ vi php_ttt.h

[/codesyntax]

PHP_FUNCTION(ttt_hello);

[codesyntax lang=”bash” lines=”no” container=”pre” tab_width=”4″]

$ vi ttt.c

[/codesyntax]

PHP_FE(ttt_hello, NULL)

PHP_FUNCTION(ttt_hello){ php_printf(“hello !!”); }

[codesyntax lang=”bash” lines=”no” container=”pre” tab_width=”4″]

$ ./configure
$ make
$ sudo make install
$ apache2ctl restart
$ vi /var/www/index.php

[/codesyntax]

[codesyntax lang=”php”]

if (extension_loaded('ttt')) {
    ttt_hello('my input string');
}

[/codesyntax]




For Windows


1. Installations and Configurations

cygwin (c:\cygwin)
vc6
Apache
PHP (D:\php-5.2.14-Win32)
PHP source (D:\php-5.2.14-source)
Add env path (D:\php-5.2.14-Win32)


2. Create Extension

[codesyntax lang=”bash” lines=”no” container=”pre” tab_width=”4″]

$ cd D:\php-5.2.14-source\ext
$ php ext_skel_win32.php --extname=test

[/codesyntax]


3. Extension Config

[codesyntax lang=”bash” lines=”no” container=”pre” tab_width=”4″]

$ notepad ext/test/php_test.h

[/codesyntax]

PHP_FUNCTION(hello);

[codesyntax lang=”bash” lines=”no” container=”pre” tab_width=”4″]

$ notepad ext/test/test.c

[/codesyntax]

PHP_FE(hello, NULL)

PHP_FUNCTION(hello){ php_printf(“hello !!”); }


4. Build Extension

[codesyntax lang=”bash” lines=”no” container=”pre” tab_width=”4″]

$ copy D:\php-5.2.14-Win32\dev\php5ts.lib D:\php-5.2.14-source\ext\test\php5ts.lib

[/codesyntax]

vc6 :

Open project -> test.dsp

Build -> Set Active Configuration choose release mode (or remove debug mode)

Build dll -> D:\php-5.2.14-source\Release_TS\php_test.dll

[codesyntax lang=”bash” lines=”no” container=”pre” tab_width=”4″]

$ copy D:\php-5.2.14-source\Release_TS\php_test.dll D:\php-5.2.14-Win32\ext\php_test.dll

[/codesyntax]


5. Run and Test

[codesyntax lang=”bash” lines=”no” container=”pre” tab_width=”4″]

$ notepad D:\php-5.2.14-Win32\php.ini

[/codesyntax]

extension_dir=”D:\php-5.2.14-Win32\ext”

extension=php_test.dll

[codesyntax lang=”bash” lines=”no” container=”pre” tab_width=”4″]

$ notepad test.php

[/codesyntax]

<?php hello(); ?>


Restart apache

Posted in PHP, Ubuntu | Tagged , | Leave a comment