Use CC ++ to extend the php language to implement the Usher_GetHostIP parameter-free function
It is also necessary for PHP programmers to slightly understand C, no matter from the business or professional perspective, because C is the native language of PHP. In addition, we generally do not use native PHP to process large volumes of business logic. in this case, we need to expand ,.
PHP is indeed a good language in essence. if it is used flexibly, it should not be a bottleneck that hinders our development, but a powerful tool.
Using PHP properly is really a good choice.
Now go to the topic:
PHP extension through C
Implementation function Usher_GetHostIP ()
This function is used to obtain the system IP address and return a call without parameters.
Go to the topic:
First, go to the ext directory of PHP source code and execute./ext_skel -- extname = usher. after the execution of the extended architecture is generated, see figure
Generated usher extension directory
Access the cd usher ls file and find the following files. I have better configuration. it doesn't matter.
Run/usr/local/php/bin/phpize and select phpize based on your path. This step is the configure generated by autoconfig.
At this step, the compilation environment is ready !! The following describes how to implement Usher_GetHostIP ()
Go to the generated usher directory and edit usher. c and php_usher.h. these two files are C header files and source files. we need to expand PHP here
I directly paste the code
Open php_usher.h and add the following code:
PHP_FUNCTION (Usher_GetHostIP); // note that this is an extension function we have added.
The result is as follows:
# Ifndef PHP_USHER_H # define PHP_USHER_Hextern zend_module_entry usher_module_entry; # define phpext_usher_ptr & usher_module_entry # define PHP_USHER_VERSION "0.1.0"/* Replace with version number for your extension */# ifdef PHP_WIN32 # define PHP_USHER_API _ declspec (dllexport) # elif defined (_ GNUC _) & _ GNUC _> = 4 # define PHP_USHER_API _ attribute _ (visibility ("default "))) # else # define PHP_USHER_API # endif # ifdef ZTS # include "TSRM. h "# login (usher); PHP_MSHUTDOWN_FUNCTION (usher); PHP_RINIT_FUNCTION (usher); PHP_RSHUTDOWN_FUNCTION (usher); PHP_MINFO_FUNCTION (usher); PHP_FUNCTION (login);/* For testing, remove later. */PHP_FUNCTION (Usher_GetHostIP); // note that this is our own extended function/* Declare any global variables you may need between the BEGINand END macros here: ZEND_BEGIN_MODULE_GLOBALS (usher) long global_value; char * global_string; random (usher) */# ifdef ZTS # define USHER_G (v) TSRMG (usher_globals_id, zend_usher_globals *, v) # else # define USHER_G (v) (usher_globals.v) # endif/* PHP_USHER_H */
Open usher. c and add the following code:
# Include
// The added extension code # include
# Include
# Include
// Usher_GetHostIP implements PHP_FUNCTION (Usher_GetHostIP) {char * arg = NULL; int arg_len, len; char * strg; struct hostent * he; char hostname [20] = {0 }; gethostname (hostname, sizeof (hostname); he = gethostbyname (hostname); if (ZEND_NUM_ARGS ()! = 0) {RETURN_STRINGL ("Prm error", strlen ("Prm error"), 0); return;} len = spprintf (& strg, 0, "% s: % s "," usher ", inet_ntoa (* (struct in_addr *) (he-> h_addr); RETURN_STRINGL (strg, len, 0 );}
The final result is as follows: usher. c:
# Ifdef HAVE_CONFIG_H # include "config. h "# endif # include" main/php. h "# include" main/php_ini.h "# include" ext/standard/info. h "# include" php_usher.h "# include
// The added extension code # include
# Include
# Include static int le_usher; const zend_function_entry usher_functions [] = {PHP_FE (Usher_GetHostIP, NULL)/* For testing, remove later. */PHP_FE (confirm_usher_compiled, NULL)/* For testing, remove later. */PHP_FE_END/* Must be the last line in usher_functions [] */}; /* }}* // * {usher_module_entry */zend_module_entry usher_module_entry = {# if ZEND_MODULE_API_NO> = 20010901STANDARD_MODULE_HEADER, # end If "usher", usher_functions, PHP_MINIT (usher), PHP_MSHUTDOWN (usher), PHP_RINIT (usher ), /* Replace with NULL if there's nothing to do at request start */PHP_RSHUTDOWN (usher ), /* Replace with NULL if there's nothing to do at request end */PHP_MINFO (usher), # if ZEND_MODULE_API_NO> = 20010901PHP_USHER_VERSION, # endifSTANDARD_MODULE_PROPERTIES }; /* }}*/# ifdef COMPILE_DL_USHERZEND_GET_MODULE (usher) # endifPHP_MIN IT_FUNCTION (usher) {return SUCCESS;} reverse (usher) {return SUCCESS;} PHP_RINIT_FUNCTION (usher) {return SUCCESS;} reverse (usher) {return SUCCESS;} PHP_MINFO_FUNCTION (usher) {php_info_print_table_start (); php_info_print_table_header (2, "usher support", "enabled"); enabled () ;}// Usher_GetHostIP implements PHP_FUNCTION (Usher_GetHostIP) {char * arg = NULL; int arg_len, len; cha R * strg; struct hostent * he; char hostname [20] = {0}; gethostname (hostname, sizeof (hostname); he = gethostbyname (hostname ); if (ZEND_NUM_ARGS ()! = 0) {RETURN_STRINGL ("Prm error", strlen ("Prm error"), 0); return;} len = spprintf (& strg, 0, "% s: % s "," usher ", inet_ntoa (* (struct in_addr *) (he-> h_addr); RETURN_STRINGL (strg, len, 0);} PHP_FUNCTION (confirm_usher_compiled) {char * arg = NULL; int arg_len, len; char * strg; if (zend_parse_parameters (ZEND_NUM_ARGS () TSRMLS_CC, "s", & arg, & arg_len) = FAILURE) {return;} len = spprintf (& strg, 0, "Congratulati Ons! You have successfully modified ext/%. 78 s/config. m4. Module %. 78 s is now compiled into PHP. "," usher ", arg); RETURN_STRINGL (strg, len, 0 );}
Compilation starts after code modification.
Modify config. m4 in the usher directory and remove the dnl (PHP_ARG_WITH, [-- with-usher) in front of the two lines)
Directly./make in the usher Directory. if the result is displayed, the compilation is successful.
Make install where to install it. see php settings. I will install it here by default.
Generated usher. so
Okay. after the extension is compiled and loaded to php, try to start fpm and enter phpinfo. check if our extension has been loaded.
See usher support enabled !!!!!
Let's write a code test in PHP:
echo Usher_GetHostIP();
The result is as follows: PHP is successfully expanded.