Saturday, April 24, 2010

Initial installations before developing php extensions.

To develop php extensions we have to be installed php in our system. Since it takes time and involve lot of works we can easily forget the steps we have to follow. Therefore I decided to document the steps I followed when setting up my development environment. I use UBUNTU 9.04 as my platform for php-tsql research works. Before we do anything it's necessary to install some development tools which require in extension development life cycle. Therefore you first install the latest versions of following packages in to you system.

1. libxml2 (which is required to compile php source code)
2. libtool
3 make
4. automake
5. autoconf
6. m4
7. bison

But remember that some of the packages may require some other once to be installed previously. If the installation stop somewhere, look at the error and install the requesting package first. Then you can install the interrupted package again. These kind of installations require a huge patience because it may take lot of time in progress printing different things and finally give you an error saying, without a particular package it cannot continue. Then you have to install that package patiently. All you have to do to install each package is,

step 1: Decompress the the package file.
step 2: Go into the package directory.
step 4: Give the commands,

./configure
make
sudo make install


Now I assume you have installed those packages. Now It's time to install apache server and php. download the sources of apache2 and php. Decompress the package files and spend some time reading readme files in those. I think most important one is INSTALL file in php. Now we're installing apache2.

step 1: Decompress the the apache2 package file.
step 2: Go into the package directory.
step 3: To configure and install apache2 give the command,

./configure --enable-so
make
sudo make install


To check whether apache2 installed correctly start the apache2 server normally. eg:

sudo /usr/local/apache2/bin/apachectl start


Open your wed browser and go to localhost, it should show the message "It Works"
To stop the server,

sudo /usr/local/apache2/bin/apachectl stop

Now apache2 is ok. Its time for php installation.

step 1: Decompress the php package file.
step 2: Go into the package directory.
step 3: To configure and install php give the command,

./configure --with-apxs2=/usr/local/apache2/bin/apxs
make
sudo make install

If these steps completed without any error, php is installed. But you have to do some more before you start using php.

step 4: To setup your php.ini file, give the command

cp php.ini-development /usr/local/lib/php.ini

step 5: You have to make sure whether apache2 is aware of the installed php. Open the file httpd.conf (it should be in /usr/local/apache2/conf directory) and check whether this kind of an entry is there in the file,

LoadModule php5_module modules/libphp5.so

If not, put that line in the file.

step 5: In that httpd.conf file put this line also in the correct position,

AddType application/x-httpd-php .php .phtml

Now php also installed correctly. To check whether php is working, copy following code to a php files body named testinfo.php and put it in the htdocs directory of apache2.

phpinfo();


Now start apache2 server and go to the URL http://localhost/testinfo.php. It should give you the phpinfo page. If so, it confirms that php also working.
Here's a good reference with more detailed description about compiling from the source,
http://www.web-tech-india.com/articles/php/compiling_php_apache/

All the best & Enjoy your extensions developing works with this new platform. There's an article on extension writing in this blog. Better to read it.

Friday, April 23, 2010

php-tsql Project : Now the tsql module talks.

Few weeks ago Mr. Laxman gave us a sample web server and a client to study and to embed the sample client in our tsql module. After huge effort now we're able to communicate with the sample web server though the tsql module. Now we can understand the way we have to implement the module one day to communicate with the tsql server. However still we couldn't implement the provided function in the module to accept parameters for the host name and port number. Those details are hard coded in the function yet. I'm still looking for the correct way to make php modules to accept parameters in functions.

Saturday, April 3, 2010

Creating a simple php extension

(This article assumes that you have installed apache2,php and all the other development tools in your machine. To do those things there's an article with all the instructions)

To make a php extension we can use the mechanism provided in the php source code which is the shell script named ext_skel. First download the latest php source code and find the ext_skel shell script in the ext directory in source code.

1. Give this command from the terminal in ext directory.

./ext_skel --extname=TikiriDB

Now a directory will be created in ext called TikiriDB. Then we can use that extension skeliton to build our module.

2. Go to the TikiriDB directory. Open the file TikiriDB.c file and add the line PHP_FE(tikiri_print, NULL) in appropriate location to see like,

const zend_function_entry TikiriDB_functions[] = {

PHP_FE(confirm_TikiriDB_compiled, NULL)

PHP_FE(tikiri_print, NULL)

{NULL, NULL, NULL} /* Must be the last line in TikiriDB_functions[] */

};

There's another thing which should be added to the same file. Add the code below at the end of the file.

PHP_FUNCTION(tikiri_print){

char *text = NULL;

/* count the number of arguments passed */

int argc = ZEND_NUM_ARGS();

int text_len;

/* TSRMLS_CC boils down to ", tsrm_ls" where tsrm_ls is a resource handler.

You don't need to worry about this, it is used to make it thread safe */

if (zend_parse_parameters(argc TSRMLS_CC, "s", &text, &text_len) == FAILURE) {

php_error_doc_ref(NULL TSRMLS_CC, E_ERROR, "Invalid Parameters");

return;

} /* Copy the value from the passed string, create a new one and return it */

RETURN_STRING(text, 1);

}

3. Now open the file php_TikiriDB.h. Add the line PHP_FUNCTION(tikiri_print); to the appropriate location to be like this.

PHP_MINIT_FUNCTION(TikiriDB);
PHP_MSHUTDOWN_FUNCTION(TikiriDB);
PHP_RINIT_FUNCTION(TikiriDB);
PHP_RSHUTDOWN_FUNCTION(TikiriDB);
PHP_MINFO_FUNCTION(TikiriDB);
PHP_FUNCTION(confirm_TikiriDB_compiled);
PHP_FUNCTION(tikiri_print);

4. Open the file config.m4 and remove the comments remain the following part uncommented.

PHP_ARG_ENABLE(TikiriDB, whether to enable TikiriDB support,

[ --enable-TikiriDB Enable TikiriDB support])

5. Now give this command inside the TikiriDB directory,

phpize

./configure --enable-TikiriDB

make

6. Now in the TikiriDB directory open the file Makefile and in the EXTENSION_DIR attribute give the path to the extension directory of your installed php.

eg- If your extensions are in the directory /usr/local/include/php/ext set the path as,

EXTENSION_DIR = /usr/local/include/php/ext

7. Now give this command inside the TikiriDB directory,

make install

8. The extension is now installed. Open your php.ini file and make sure the extension_dir attribute is set to the same path.

Add an entry to it as,

extension=TikiriDB.so

9. Now in your apache servers htdocs directory put a file named testing.php and put the content below in that folder.

$string = tikiri_print("Hello World");
echo $string;

10. Start your apache server and access the file testing.php from the web browser. The function tikiri_print returns the string given to it.

If you want to change the functions defined in the module several time and test again and again, you have to repeat the steps,


/usr/local/bin/phpize
./configure --enable-TikiriDB
make


Copy the new TikiriDB.so file in the modules folder of the TikiriDB folder to the extension directory of the installed php's extension directory overwriting the old TikiriDB.so file.