Php: how to call the c interface without error _ PHP Tutorial

Source: Internet
Author: User
Php calls the c interface. Php calls the c interface. first of all to install PHP2. PHP directory to download and unzip the ext Directory # cdrootphp-5.3.6ext #. ext_skel -- extnamehmc description :. ext_skel -- php: no error in calling c interface

1. install PHP

2. go to the ext directory under the PHP download and decompress directory.

# Cd/root/php-5.3.6/ext

#./Ext_skel -- extname = hmc

Note:

./Ext_skel -- extname = module_name
Module_name is the name of the extension module you can choose, for example, the hmc I selected. After the tool is executed, the directory named module_name of your choice is automatically created under the ext directory, which generates the relevant code. in the code, you only need to adjust the config. the three lines of comments in the m4 file can be used to compile php with the custom extension module normally.

3. modify config. m4

You can copy the generated module directory to any location you want to place.

Go to the hmc Directory

Vi config. m4

Open the config. m4 file in the text editor.

Choose

Dnl PHP_ARG_WITH (hmc, for hmc support,

Dnl Make sure that the comment is aligned:

Dnl [-- with-hmc Include hmc support])

Modify

PHP_ARG_WITH (hmc for hmc support,

Make sure that the comment is aligned:

[-- With-hmc Include hmc support])

Or set

Dnl PHP_ARG_ENABLE (hmc, whether to enable hmc support,

Dnl Make sure that the comment is aligned:

Dnl [-- enable-hmc Enable hmc support])

Modify

PHP_ARG_ENABLE (hmc, whether to enable hmc support,

Make sure that the comment is aligned:

[-- Enable-hmc Enable hmc support])



Generally, I will select the latter and save and exit.

4. modify the. c and. h file codes.

Vi hmc. c

Modify the following code in the file.

/* Every user visible function must have an entry in hmc_functions [].

*/

Const zend_function_entry hmc_functions [] = {

PHP_FE (say_hello, NULL)/* Add a line of code */

PHP_FE (confirm_hmc_compiled, NULL)/* For testing, remove later .*/

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

};



Add the following code at the end of the file

PHP_FUNCTION (say_hello)

{

Zend_printf ("hello world \ n ");

}

Save the file and exit



Vi php_hmc.h

In the file PHP_FUNCTION (confirm_hmc_compiled); add the following code in front of a line

PHP_FUNCTION (say_hello );

Save the file and exit

5. Compile and install

# Phpize
#./Configure -- enable-hmc

# Make

# Make install

There should be an hmc. so file in the modules directory of php. different machine locations will be different.

6. modify PHP configuration

Edit php. ini and add the extension:
Add the following in the [PHP] module:
Extension = hmc. so
Restart Httpd

# Service httpd restart

7. php call

Create a c. php file under the corresponding www Directory

Say_hello ();
?>

In this case, the browser should output:

Hello world

8. debugging

You can enter

# Php-q c. php

Enter the current www Directory

Check whether the module has been installed. you can use phpinfo ()

You can also use the php-m command.

First use

# Which php

Find the directory where php is located. different machine directories are located.

#/Usr/bin/php-m

9. functions with parameters

Vi hmc. c

The last modified say_hello function is as follows:

PHP_FUNCTION (say_hello)
{
Zval ** yourname;
If (ZEND_NUM_ARGS ()! = 1 | zend_get_parameters_ex (1, & yourname) = FAILURE)

{
WRONG_PARAM_COUNT;
}
Zend_printf ("hello world, % s \ n", Z_STRVAL_PP (yourname ));
}


Modify c. php

Say_hello ("clear moon ");
?>

Lifecycle 1. first, install PHP 2. go to the ext directory under the PHP download unzip Directory # cd/root/php-5.3.6/ext #. /ext_skel -- extname = hmc description :. /ext_skel --...

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.