Tuesday, August 24, 2010

Linking C libraries to php extensions

After learning to create a simple php module it took a long time for me to understand the correct way of porting an existing C library to to a php module. Sometimes I thought it may be impossible for me. After all I have managed to understand the correct way of doing it. If you need to learn about creating a simple php module, follow this article. So, here's the procedure I followed to build the php module which is linked to a C library.

1) First step is creating a simple C library to link to a php module. So, write and save the following source files.

Here's the "asanka.c" file,

#include "asanka.h"

void setValue(int a){
value = a;

int getValue(){
return value;

void increment(){
value = value + 1;

void decrement(){
value = value - 1;

Here's the "asanka.h" file,


int value;

void setValue(int a);

int getValue();

void increment();

void decrement();

Now create the C library called "libasanka.a" by giving the command,

gcc -c asanka.c
ar -cr libasanka.a asanka.o

Ok, now we have the library. Create a directory in some place, lets's say in your home directory as the "library". Inside that directory, create two directories as "lib" and "include". Put the library file "libasanka.a" into the "lib" directory and "asanka.h" file to the "include" directory. So, now the paths to both should be,


There's a reason for giving the path's of the files. We have to give the path when we create the php module.

2) Now it's time to create the php module. Goto ext directory of the php source and give the command in the terminal,

./ext_skel --extname=asanka

3) Now go into the newly created directory "asanka" inside the ext directory and open the auto generated file "asanka.c" (this is not the file we created). Add the following entries in the appropriated places,

PHP_FE(asanka_setValue, NULL)
PHP_FE(asanka_getValue, NULL)
PHP_FE(asanka_increment, NULL)
PHP_FE(asanka_decrement, NULL)

Add the following entries also in the appropriate place in the file,

int locValue;
int argc = ZEND_NUM_ARGS();

if (zend_parse_parameters(argc TSRMLS_CC, "l", &locValue) == FAILURE) {
php_error_doc_ref(NULL TSRMLS_CC, E_ERROR, "Invalid Parameters");


int locValue;
locValue = getValue();



4) Now open the file "php_asanka.h" in the same directory and add the following to it,


5) Open the config.m4 file and add change it's content to be match with the following code,

dnl $Id$
dnl config.m4 for extension asanka

dnl Comments in this file start with the string 'dnl'.
dnl Remove where necessary. This file will not work
dnl without editing.

dnl If your extension references something external, use with:

PHP_ARG_WITH(asanka, for asanka support,
[ --with-asanka[=DIR] Include asanka support])

dnl Otherwise use enable:

dnl PHP_ARG_ENABLE(asanka, whether to enable asanka support,
dnl Make sure that the comment is aligned:
dnl [ --enable-asanka Enable asanka support])

if test "$PHP_ASANKA" != "no"; then
dnl Write more examples of tests here...

dnl # --with-asanka -> check with-path
dnl SEARCH_PATH="/usr/local /usr" # you might want to change this
dnl SEARCH_FOR="/include/asanka.h" # you most likely want to change this
if test -r $PHP_ASANKA/lib/libasanka.a; then # path given as parameter
else # search default path list
AC_MSG_CHECKING([for asanka files in default path])
for i in /usr/local /usr; do
if test -r $i/lib/libasanka.a; then
AC_MSG_RESULT(found in $i)
if test -z "$ASANKA_DIR"; then
AC_MSG_RESULT([not found])
AC_MSG_ERROR([Please reinstall the libasanka distribution - asanka.h should be /include and libasanka.a should be in /lib])

dnl # --with-asanka -> add include path

dnl # --with-asanka -> check for lib and symbol presence
LIBNAME=asanka # you may want to change this
LIBSYMBOL=getValue # you most likely want to change this

AC_MSG_ERROR([wrong asanka lib version or lib not found])
-L$ASANKA_DIR/lib -lm

PHP_NEW_EXTENSION(asanka, asanka.c, $ext_shared)

6) Now run the following commands in terminal inside the "asanka" directory,


./configure --with-asanka=/home/username/library/

sudo make

7) Now our new module is ready. You can find it in the "modules" directory inside the "asanka" directory. Copy that "asanka.so" shared object to the extensions directory of your installed php. If you don't know the path to the extensions directory, look at the phpinfo() page.

8) After placing "asanka.so" into the extensions directory, next thing is editing the php.ini file. Open it and add an entry as follows in the appropriate place,


9) Now every things ok. Write a php file with the following content and place it in the "htdocs" directory of the apache server,

echo asanka_getValue();

Check whether your web browser displays the correct output. If you see number 12, then your C library works correctly with the php module.

Saturday, August 21, 2010

Fundamental issue in the integration of php-tsql module and tikirisql library.

I think we've faced with a serious problem in works. Yesterday night I was working on integration of the php-tsql module with the tikirisql library. I was faced with a situation where it's hard to compile the integrated php module since it's makefile is an auto generated one. I think our tikirisql library makes us troubles since it's coded for a general purpose. I directly called the tikirisql's functions inside the php-tsql module. Then I compiled and placed it in my php extensions directory. When I try to run a testing php script in apache server which uses our php modules functions, the php file's going to be downloaded without rendering on the browser.
I think most probably the reason should be the usage of tikirisql's C files and header files within the php module. There may be some restrictions by the creators of php about the way we create extensions to the php.
Sometimes the same problem may have been encountered by the MySQL community who use a very simple php module which does only the exchanging of the web client's queries to MySQL server and results back to clients browser. They doesn't do parsing stuff in php module. I looked at the mysql module in php. It's very simple with only the standard source files. Those standard files are in each and every module in php. As I understand everything the mysql people has changed is the content in those source files. They have written new functions and called them. Therefore I think there may be a big reason for not using external libraries inside php modules.
I think we have to study more about the php internals to understand the problem and to find a possible solution.

Tuesday, August 17, 2010

A php progress bar

From several days I was trying to add a progress bar to the FIT4D's acquiring disk image front end. Even though I couldn't finish it, I thought to write a note about the things I learned from it. I found a php progress bar from here http://webscripts.softpedia.com/script/PHP-Clases/PHP-Progressbar-25825.html. Then I included the following code in a php file and opened it with browser.

require_once 'ProgressBar.class.php';
$bar = new ProgressBar();
$elements = 1000000; //total number of elements to process
$bar->initialize($elements); //print the empty bar
for($i=0;$i<$elements;$i++){ //do something here... $bar->increase(); //calls the bar with every processed element

Then it worked as the developer of it mentioned. Now the hard part. I wanted to include it in our front end. But even though I included the above code segment inside the create_img2Main.php file in the lib directory of the FIT4D source, it doesn't show the dynamic progress bar. Instead, it suddenly shows the final state of the progress bar which is 100% filled.
According to my understanding, I think this is because the file create_img2Main.php is loaded using an ajax request from create_imgMain.php through functions in global.js file. I hope to discuss about the issue with Yasantha akka tomorrow.