How to write PHP extensions in C language in the detailed _php skills

Source: Internet
Author: User
Tags function definition function prototype php source code zts

1: Predefined
In the home directory, you can also write a file in any other directory, such as Caleng_module.def
The content is the name of the function you want to define and the parameters:
int a (int x,int y)
String B (String str,int N)

2: To the PHP source directory of the Ext directory
#cd/usr/local/php-5.4.0/ext/

Execute command to generate corresponding extended directory
#./ext_skel--extname=caleng_module--proto=/home/hm/caleng_module.def

3: Modify the CONFIG.M4
Get rid of DNL notes

Php_arg_enable (Caleng_module, whether to ENABLE caleng_module support,
Make sure this comment is aligned:
[--enable-caleng_module enable caleng_module support])

4: Modify the CALENG_MODULE.C

Copy Code code as follows:

/* {{proto int a (int x, int y)
*/
Php_function (a)
{
int argc = Zend_num_args ();
int x;
int y;
int z;
if (Zend_parse_parameters (argc tsrmls_cc, "ll", &x, &y) = = failure)

Return
Z=x+y;
Return_long (z);
}
/* }}} */
/* {{proto string B (string str, int n)
*/
Php_function (b)
{
char *str = NULL;
int argc = Zend_num_args ();
int str_len;
Long N;
Char *result;
Char *ptr;
int result_length;

if (Zend_parse_parameters (argc tsrmls_cc, "SL", &str, &str_len, &n) = = failure)
Return
Result_length = Str_len * N;
result = (char *) emalloc (result_length + 1);
ptr = result;
while (n--) {
memcpy (PTR, str, str_len);
PTR + = Str_len;
}
*ptr = ' n ';
Return_stringl (result, result_length, 0);
}
/* }}} */

5: Build the extension library
#cd./caleng_module
#/usr/local/php/bin/phpize
#./configure--with-php-config=/usr/local/php/bin/php-config
#make
#make Install



6: to the corresponding extensions directory of PHP
As shown in the figure above
#cd/usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/
There are generated caleng_module.so files under the modified directory

7: Modify the PHP.ini
PHP.ini If you cannot find a message that can be typed from phpinfo ()
#cd/usr/local/php/lib/
PHP.ini Add extended information
Extension=caleng_module.so

8: Restart Apache
#/usr/local/apache2/bin/apachectl Restart

9: Check Load
/usr/local/php/bin/php-m

10:php Call

Copy Code code as follows:

echo A (1,2);

Output 3 is a success!
=================================
The following is the original
Linux Development with C PHP extension
First, download the PHP source code package, assuming the source package directory:/software/php-5.2.13
First, download the PHP source code package, assuming the source package directory:/software/php-5.2.13
#> Cd/software/php-5.2.13/ext
Second, suppose we want to develop an extension called Caleng_module, which contains two functions: a--processing Two integer addition and B-processing string repeat output;
1, first write a function definition file, the file to write the function prototype suffix to Def, assuming: caleng_module.def
int a (int x, int y)
String B (String str, int n)
2, through the extension skeleton generator, will be in the EXT directory automatically set up the extended directory Caleng_module
#>./ext_skel--extname=caleng_module--proto=caleng_module.def
3, modify the configuration file: #> vim/software/php-5.2.13/ext/caleng_module/config.m4, the following line of the annotation label "DNL" removed, the following changes are as follows:
Php_arg_enable (myfunctions, whether to ENABLE myfunctions support,
Make sure this comment is aligned:
[--enable-myfunctions enable myfunctions support])
4, the function of perfecting function A and B: #> vim/software/php-5.2.13/ext/caleng_module/caleng_module.c
Php_function (a)
{
int x, y, Z;

int argc = Zend_num_args ();

if (Zend_parse_parameters (argc tsrmls_cc, "ll", &x, &y) = = failure)
Return
z = x + y;
Return_long (z);
}
Php_function (b)
{
char *str = NULL;
int argc = Zend_num_args ();
int str_len;
Long N;
Char *result;
Char *ptr;
int result_length;

if (Zend_parse_parameters (argc tsrmls_cc, "SL", &str, &str_len, &n) = = failure)
Return
Result_length = Str_len * N;
result = (char *) emalloc (result_length + 1);
ptr = result;
while (n--) {
memcpy (PTR, str, str_len);
PTR + = Str_len;
}
*ptr = ' n ';
Return_stringl (result, result_length, 0);
}
Third, compile the installation, assuming PHP installation directory is:/usr/localhost/webserver/php
#> Cd/software/php-5.2.13/ext/caleng_module
#>/usr/localhost/webserver/php/bin/phpize
#>./configure--with-php-config=/usr/localhost/webserver/php/bin/php-config
#> make
#> make Install
The caleng_module.so file will now be generated in the/usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20060613 directory
In the php.ini configuration file, add: extension=caleng_module.so.
Finish the call.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.