Use PHP to invoke code in the so library file and write so simple instances
If a feature is compiled into the so file, then how to invoke it through PHP. One way to do this is to write a php extension module (PHP extension), call the function within the module in PHP, and then call the function in so through the module. The following is a simple example of an operating system that uses RHEL5.
First, make a simple so file:
/**
* HELLO.C
* To compile, use following commands:
* Gcc-o-c-fpic-o hello.o hello.c
* Gcc-shared-o libhello.so hello.o
*/
int Hello_add (int a, int b)
{
return a + B;
And then compile it into the. so file and put it into the system:
$ gcc-o-c-fpic-o hello.o hello.c
$ gcc-shared-o libhello.so hello.o
$ su
# Echo/usr/local/lib >/etc/ld.so.conf.d/local.conf
# CP Libhello.so/usr/local/lib
#/sbin/ldconfig Write a small program to verify its correctness:
/**
* HELLOTEST.C
* To compile, use following commands:
* Gcc-o Hellotest-lhello hellotest.c
*/
#i nclude <stdio.h>
int main ()
{
int a = 3, b = 4;
printf ("%d +%d =%d/n", A, B, Hello_add (a,b));
return 0;
Compile and execute:
$ gcc-o Hellotest-lhello hellotest.c
$./hellotest
3 + 4 = 7OK, let's Make a PHP module below. First make sure you install the Php-devel package, if not, please find it by yourself from the installation CD. Then download the PHP source code. I'm using php-5.2.3.tar.gz, decompression.
$ tar xzvf php-5.2.3.tar.gz
$ CD Php-5.2.3/ext then create a module called Hello with the following command.
$./ext_skel--extname=hello After executing the command, it prompts you what commands should be used to compile the module, but it is a compilation method that integrates the module into the PHP interior. The method is simpler if you want to compile into a php_hello.so that can be dynamically loaded.
$ cd Hello first edit the config.m4 file and remove the comments from lines 16th and 18th (the annotation symbol is DNL.) )
16:php_arg_enable (Hello, whether to ENABLE hello support,
17:DNL Make sure this comment is aligned:
[--enable-hello enable hello support] then execute the PHPIZE program to generate the Configure script:
$ phpize then opens Php_hello.h, in Php_function (confirm_hello_compiled); Add function declaration below:
Php_function (confirm_hello_compiled); /* For testing, remove later. */
Php_function (Hello_add); open hello.c, add the following below Php_fe (confirm_hello_compiled, NULL).
Zend_function_entry hello_functions[] = {
Php_fe (confirm_hello_compiled, NULL)/* For testing, remove later. */
Php_fe (Hello_add, NULL)/* For testing, remove later. */
{null, NULL, NULL}/* must is the last line in hello_functions[] * *
And then write the contents of the Hello_add function at the very end of the hello.c:
Php_function (Hello_add)
{
Long int A, b;
long int result;
if (Zend_parse_parameters (Zend_num_args () tsrmls_cc, "ll", &a, &b) = = failure) {
Return
}
result = Hello_add (A, b);
Return_long (result);
Save exit, compile and install:
$./configure
$ make Ldflags=-lhello
$ su
# CP Modules/hello.so/usr/lib/php/modules Then create a hello.php file under/var/www/html, which reads as follows:
<?php
DL ("hello.so");
Echo Hello_add (3, 4);
?> then opens the hello.php file in the browser, and if 7 is displayed, the function call succeeds.
This article from Csdn Blog, reproduced please indicate the source: http://blog.csdn.net/zuiaituantuan/archive/2010/10/03/5919572.aspx