PHP programmers need to be slightly c is also necessary, whether from a business perspective or professional perspective, because C is the native language of PHP. And we generally do not use native PHP to deal with high-volume business logic, this time we need to expand.
PHP from the essence of language is really a good language, if the use of flexible, he should not be a bottleneck to hinder our progress, but rather a weapon.
It's really a good choice if you use PHP properly.
Now get to the point:
Extending PHP through C
Implement function Usher_gethostip ()
The function is to get the system IP and return a parameterless call
Go straight to the chase:
First enter the PHP source code in the Ext directory to execute./ext_skel--extname=usher This step produces an extended schema after execution as
Generated the Usher extension directory
Enter the CD Usher LS found the following file I'm here to configure better than everyone cares
Run/usr/local/php/bin/phpize According to your path select Phpize This step is autoconfig the use of the resulting configure
To this step OK compile environment ready to finish!! Below we start to implement our Usher_gethostip ()
into the resulting Usher directory edit usher.c and php_usher.h These two files are C header and source files we need to extend PHP here
I'm just pasting the code.
Open Php_usher.h Add the following code
Php_function (Usher_gethostip); Notice here that this is the extension function we added ourselves.
The results are 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#D Efine 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" #endifPHP_MINIT_ FUNCTION (Usher); Php_mshutdown_function (Usher); Php_rinit_function (Usher); Php_rshutdown_function (Usher); Php_minfo_function (Usher); Php_function (confirm_usher_compiled);/* For testing, remove later. */php_function (Usher_gethostip); Note here This is the extension function that we add ourselves/* Declare any global variables your may need between the Beginand END macros Here:zend_begin_ Module_globals (Usher) long Global_value;char *global_string; Zend_end_module_globals (Usher) */#ifdef Zts#define usher_g (v) tsrmg (usher_globals_id, zend_usher_globals *, v) #else # Define Usher_G (v) (USHER_GLOBALS.V) #endif #endif/* php_usher_h */
Open USHER.C Add the following code
#include <netdb.h> //Extension code We added # include <sys/socket.h> #include <netinet/in.h> #include < Arpa/inet.h>
Usher_gethostip implementation 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 usher.c are as follows
#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 <netdb.h>//Extension code We added # include <sys/socket.h> #include <netinet/ in.h> #include <arpa/inet.h>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 is the last line in usher_functions[] */};/*}}} *//* {{{usher_module_entry */zend_module_entry Usher _module_entry = {#if zend_module_api_no >= 20010901standard_module_header, #endif "Usher", Usher_functions,php_ Minit (Usher), Php_mshutdown (Usher), Php_rinit (Usher),/* Replace with NULL if there ' s nothing to does at request start */php_rs Hutdown (Usher),/* Replace with NULL If there's nothing to does 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_MINIT_FUNCTION (Usher) {return SUCCESS;} Php_mshutdown_function (Usher) {return SUCCESS;} Php_rinit_function (Usher) {return SUCCESS;} Php_rshutdown_function (Usher) {return SUCCESS;} Php_minfo_function (Usher) {Php_info_print_table_start ();p Hp_info_print_table_header (2, "Usher support", "enabled") ;p hp_info_print_table_end ();} Usher_gethostip implementation 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);} 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, "congratulations! You have successfully modified EXT/%.78S/CONFIG.M4. Module%.78s is now compiled into PHP. "," Usher ", Arg); Return_stringl (STRG, Len, 0);}
Code modified to start compiling
Modify CONFIG.M4 in the Usher directory to remove the DNL before the two lines shown (Php_arg_with, [--with-usher the two lines of DNL)
Direct./make in the Usher directory as a result of successful compilation
Make install installation where to look at the PHP settings I installed here by default to
Generated a usher.so
Okay, the extension is finished. Load to PHP try starting fpm into phpinfo view our extension loaded no
Let's see. Usher support Enabled!!!!!
Let's write a code test in PHP:
Echo Usher_gethostip ();
As a result, we have successfully expanded PHP
Implementation of Usher_gethostip parameterless function with C + + extended PHP language