ZooKernel/Embed/PHP

Version 3 (modified by nicboz, 14 years ago)

--

ZooKernel/Embed/PHP description page

You get lot of existing php code and need to use it as ZOO Service with the less modifications possible ? Then this page is made for you :)

On this page, you will find advices to implement your services directly in PHP.

Configure and Install PHP Embed library

First thing you need is a fresh and independant php library installed on your system.

Here is a sample configure call to ensure the PHP support on your platform :

./configure' '--prefix=/usr/lib/php5.2.10' \
             '--host=i686-pc-linux-gnu'\
             '--mandir=/usr/lib/php5.2.10/man'\
             '--infodir=/usr/lib/php5.2.10/info'\
             '--sysconfdir=/etc'\
             '--cache-file=./config.cache'\
             '--with-pcre-regex=/usr'\
             '--enable-maintainer-zts'\
             '--with-config-file-path=/etc/php/apache2-php5.2.10'\
             '--with-config-file-scan-dir=/etc/php/apache2-php5.2.10/ext-active'\
             '--without-pear'\
             '--disable-bcmath'\
             '--with-bz2'\
             '--disable-calendar'\
             '--with-curl'\
             '--without-curlwrappers'\
             '--disable-dbase'\
             '--enable-exif'\
             '--without-fbsql'\
             '--without-fdftk'\
             '--disable-filter'\
             '--enable-ftp'\
             '--with-gettext'\
             '--without-gmp'\
             '--disable-hash'\
             '--without-kerberos'\
             '--enable-mbstring'\
             '--with-mcrypt'\
             '--without-mhash'\
             '--without-msql'\
             '--without-mssql'\
             '--with-ncurses'\
             '--with-openssl'\
             '--with-openssl-dir=/usr'\
             '--disable-pcntl'\
             '--disable-pdo'\
             '--with-pgsql'\
             '--disable-posix'\
             '--without-pspell'\
             '--without-recode'\
             '--disable-shmop'\
             '--without-snmp'\
             '--enable-soap'\
             '--enable-sockets'\
             '--without-sybase'\
             '--without-sybase-ct'\
             '--disable-sysvmsg'\
             '--disable-sysvsem'\
             '--disable-sysvshm'\
             '--without-tidy'\
             '--enable-wddx'\
             '--with-xmlrpc'\
             '--with-xsl'\
             '--enable-zip'\
             '--with-zlib'\
             '--disable-debug'\
             '--enable-dba'\
             '--without-cdb'\
             '--with-db4'\
             '--disable-flatfile'\
             '--with-gdbm'\
             '--disable-inifile'\
             '--without-qdbm'\
             '--with-freetype-dir=/usr'\
             '--with-t1lib=/usr'\
             '--disable-gd-jis-conv'\
             '--with-jpeg-dir=/usr'\
             '--with-png-dir=/usr'\
             '--with-xpm-dir=/usr'\
             '--with-gd'\
             '--without-mysqli'\
             '--with-readline'\
             '--without-libedit'\
             '--without-mm'\
             '--with-sqlite=/usr'\
             '--enable-sqlite-utf8'\
             '--with-pic'\
             '--enable-embed'

As you could seen above, there are lot of configure options used. Lots options are not required. Nevertheless, two configure options are required to get a full PHP support in your ZooKernel : --enable-embed and '--enable-maintainer-zts. You should also note that the php library will be installed in the /usr/lib/php5.2.10 directory, as we need a well sperated installation of php to not interfere with your possibly allready installed PHP version.

Once you configure script end with success, you're now ready to compile and install the php embed library, using the following commands :

make 
make test
make install

Simple PHP scripts loader

Here is the simplest code snipest ever shared. It was provided here as a sample to load and run php script from a C program :

/**
  * Filename : test_phpembed.c
  */ 
#include <stdio.h>
#include <sapi/embed/php_embed.h>
#include <zend_stream.h>

int main(int argc,char*argv[]){
  zend_file_handle script;
  if(argc<=1){
    fprintf(stderr,"Usage: embed filename.php <arguments>\n");
    return -1;
  }

  script.type=ZEND_HANDLE_FP;
  script.filename=argv[1];
  script.opened_path=NULL;
  script.free_filename=0;
  if(!(script.handle.fp=fopen(script.filename,"rb"))){
    fprintf(stderr,"Unable to open : %s\n",argv[1]);
    return -1;
  }
  
  argc--;
  argv++;

  PHP_EMBED_START_BLOCK(argc,argv)
    php_execute_script(&script TSRMLS_CC);
  PHP_EMBED_END_BLOCK()

  return 0;
}

Compile the code snipest

Now that you get the file test_phpembed.c, you must compile it using the following commands :

gcc -o test_phpembeded.o test_phpembeded.c -c $(/usr/lib/php5.2.10/bin/php-config --includes)
gcc -o test_phpembeded test_phpembeded.o $(/usr/lib/php5.2.10/bin/php-config --ldflags) $(/usr/lib/php5.2.10/bin/php-config --libs) -L /usr/lib/php5.2.10/lib/ -lphp5

You should succeed runing the command above, if not please open a ticket to inform us that there are mistakes in those instructions :)

Run the code snipest

The last step to get your PHP embeded in your C program is to write a small php piece a .... code :)

cat > info.php <<EOF
<? phpinfo(); ?>
EOF

Here we are, we now run the test_phpembed program to see if it's able to load and run the php script called info.php :

LD_LIBRARY_PATH="/usr/lib/php5.2.10/lib/" ./test_phpembeded /var/www/localhost/htdocs/info.php | less

Note that we have to set the LD_LIBRARY_PATH at runtime as we didn't install the php embed library in a standard way (not in the standard search path - /usr/lib or /usr/local/lib- to ensure that we don't interfer with your allready installed php library). Neevertheless, when ZooKernel will be compiled you only have ot run it once, so you could use a small batch file including this LD_LIBRARY_PATH and other required parameters when you launch your ZooKernel instance (note that firefox do something like that also).

Search

ZOO Sponsors

http://www.zoo-project.org/trac/chrome/site/img/geolabs-logo.pnghttp://www.zoo-project.org/trac/chrome/site/img/neogeo-logo.png http://www.zoo-project.org/trac/chrome/site/img/apptech-logo.png http://www.zoo-project.org/trac/chrome/site/img/3liz-logo.png http://www.zoo-project.org/trac/chrome/site/img/gateway-logo.png

Become a sponsor !

Knowledge partners

http://www.zoo-project.org/trac/chrome/site/img/ocu-logo.png http://www.zoo-project.org/trac/chrome/site/img/gucas-logo.png http://www.zoo-project.org/trac/chrome/site/img/polimi-logo.png http://www.zoo-project.org/trac/chrome/site/img/fem-logo.png http://www.zoo-project.org/trac/chrome/site/img/supsi-logo.png http://www.zoo-project.org/trac/chrome/site/img/cumtb-logo.png

Become a knowledge partner

Related links

http://zoo-project.org/img/ogclogo.png http://zoo-project.org/img/osgeologo.png