C语言,PHP扩展开发,注册方法
c语言代码:
/* test_add extension for PHP */ #ifdef HAVE_CONFIG_H # include "config.h" #endif #include <stdlib.h> #include <stdio.h> #include <string.h> #include <unistd.h> #include "php.h" #include "zend_exceptions.h" #include "ext/standard/info.h" #include "php_test_add.h" /* For compatibility with older PHP versions */ #ifndef ZEND_PARSE_PARAMETERS_NONE #define ZEND_PARSE_PARAMETERS_NONE() \ ZEND_PARSE_PARAMETERS_START(0, 0) \ ZEND_PARSE_PARAMETERS_END() #endif PHP_FUNCTION(xuxiaobo_test1) { ZEND_PARSE_PARAMETERS_NONE(); php_printf("已经加载了扩展:%s\n", "test_add"); } PHP_FUNCTION(xuxiaobo_test2) { char *var = "世界"; size_t var_len = sizeof("世界") - 1; zend_string *retval; ZEND_PARSE_PARAMETERS_START(0, 1) Z_PARAM_OPTIONAL Z_PARAM_STRING(var, var_len) ZEND_PARSE_PARAMETERS_END(); retval = strpprintf(0, "你好,%s", var); RETURN_STR(retval); } PHP_FUNCTION(xuxiaobo_test3) { long a, b; zend_long retval; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ll", &a, &b) == FAILURE) { RETURN_NULL(); } long c = 0; c = a + b; retval = c; RETURN_LONG(retval); } PHP_FUNCTION(xuxiaobo_test4) { char *arg = NULL; char *ret = NULL; size_t arg_len; zend_string *strg; zend_string *retval; if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &arg, &arg_len) == FAILURE) { RETURN_NULL(); // 如果参数解析失败,返回 NULL } strg = zend_string_init(arg, arg_len, 0); char *strgstr = strg->val; ret = getenv(strgstr); retval = strpprintf(0, "%s", ret); RETURN_STR(retval); } PHP_FUNCTION(xuxiaobo_test5) { char *arg1, *arg2, *arg3; size_t arg_len1, arg_len2, arg_len3; zend_string *result; ZEND_PARSE_PARAMETERS_START(3, 3) Z_PARAM_STRING(arg1, arg_len1) Z_PARAM_STRING(arg2, arg_len2) Z_PARAM_STRING(arg3, arg_len3) ZEND_PARSE_PARAMETERS_END(); result = strpprintf(0, "%s%s%s", arg1, arg2, arg3); RETURN_STR(result); } PHP_FUNCTION(xuxiaobo_test6) { zend_long num1, num2, num3, num4; // 检查参数个数 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "llll", &num1, &num2, &num3, &num4) == FAILURE) { RETURN_NULL(); } // 输出数字 array_init(return_value); add_index_long(return_value, 0, num1); add_index_long(return_value, 1, num2); add_index_long(return_value, 2, num3); add_index_long(return_value, 3, num4); add_index_long(return_value, 4, num1 + num2 + num3 + num4); add_index_long(return_value, 5, num1 * num2 * num3 * num4); } PHP_FUNCTION(xuxiaobo_test7) { zval *param1; zval *param2; zval *param3; zend_string *result; // 检查参数的数量 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zzz", ¶m1, ¶m2, ¶m3) == FAILURE) { RETURN_NULL(); } long num1 = Z_LVAL_P(param1); char str1[1024]; sprintf(str1, "%s", Z_STRVAL_P(param2)); char str2[1024]; sprintf(str2, "%s%s", Z_STRVAL_P(param2), Z_STRVAL_P(param2)); double float_value1 = Z_DVAL_P(param3); // 输出三个 array_init(return_value); add_index_long(return_value, 0, num1); add_index_string(return_value, 1, str1); add_index_double(return_value, 2, float_value1); add_index_long(return_value, 3, num1 * num1); add_index_string(return_value, 4, str2); add_index_double(return_value, 5, float_value1 * 2); } PHP_FUNCTION(xuxiaobo_test8) { } PHP_RINIT_FUNCTION(test_add) { #if defined(ZTS) && defined(COMPILE_DL_TEST_ADD) ZEND_TSRMLS_CACHE_UPDATE(); #endif return SUCCESS; } PHP_MINFO_FUNCTION(test_add) { php_info_print_table_start(); php_info_print_table_header(2, "test_add support", "enabled"); php_info_print_table_end(); } ZEND_BEGIN_ARG_INFO(arginfo_xuxiaobo_test1, 0) ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO(arginfo_xuxiaobo_test2, 0) ZEND_ARG_INFO(0, str) ZEND_END_ARG_INFO() static const zend_function_entry test_add_functions[] = { PHP_FE(xuxiaobo_test1, NULL) PHP_FE(xuxiaobo_test2, NULL) PHP_FE(xuxiaobo_test3, NULL) PHP_FE(xuxiaobo_test4, NULL) PHP_FE(xuxiaobo_test5, NULL) PHP_FE(xuxiaobo_test6, NULL) PHP_FE(xuxiaobo_test7, NULL) PHP_FE(xuxiaobo_test8, NULL) PHP_FE_END }; zend_module_entry test_add_module_entry = { STANDARD_MODULE_HEADER, "test_add", /* Extension name */ test_add_functions, /* zend_function_entry */ NULL, /* PHP_MINIT - Module initialization */ NULL, /* PHP_MSHUTDOWN - Module shutdown */ PHP_RINIT(test_add), /* PHP_RINIT - Request initialization */ NULL, /* PHP_RSHUTDOWN - Request shutdown */ PHP_MINFO(test_add), /* PHP_MINFO - Module info */ PHP_TEST_ADD_VERSION, /* Version */ STANDARD_MODULE_PROPERTIES }; #ifdef COMPILE_DL_TEST_ADD # ifdef ZTS ZEND_TSRMLS_CACHE_DEFINE() # endif ZEND_GET_MODULE(test_add) #endif
php代码:
<?php header("Content-type:text/html;charset=utf-8"); error_reporting(E_ALL); ini_set('display_errors', 1); ini_set('memory_limit', '-1'); set_time_limit(0); $arr1 = ['蒋光头', '卧槽', '你', '大爷的', '函数', '是一个非常', '实用的字符', '串操作', '工具', '它在', '各种', '场景中', '都能', '派上用场', '掌握', '函数和其他', '字符串操作', '函数', '我们', '编程', '世界中', '更加', '游刃', '有余']; echo "<title>C语言,PHP扩展开发</title>\n\n"; echo "<h1>C语言,PHP扩展开发</h1>\n\n"; echo "<b>1、xuxiaobo_test1() : </b><pre>\n"; xuxiaobo_test1(); echo "</pre>\n\n"; echo "<b>2、xuxiaobo_test2() : </b>\n"; echo "<pre>".xuxiaobo_test2()."</pre>\n\n"; $a = $arr1[random_int(0, count($arr1)-1)]; echo "<b>3、xuxiaobo_test2(\"{$a}\") : </b>\n"; echo "<pre>".xuxiaobo_test2($a)."</pre>\n\n"; $a = random_int(1, 100); $b = random_int(1, 100); $c = xuxiaobo_test3($a, $b); echo "<b>4、xuxiaobo_test3({$a}, {$b}) : </b>\n"; echo "<pre>{$c}</pre>\n\n"; $arr = ['HOME', 'USER', 'PATH']; $a = $arr[random_int(0, count($arr)-1)]; echo "<b>5、xuxiaobo_test4(\"{$a}\") : </b>\n"; echo "<pre>".xuxiaobo_test4($a)."</pre>\n\n"; $a = $arr1[random_int(0, count($arr1)-1)]; $b = $arr1[random_int(0, count($arr1)-1)]; $c = $arr1[random_int(0, count($arr1)-1)]; $d = xuxiaobo_test5($a, $b, $c); echo "<b>6、xuxiaobo_test5(\"{$a}\", \"{$b}\", \"{$c}\") : </b>\n"; echo "<pre>".$d."</pre>\n\n"; $a = random_int(1, 10); $b = random_int(1, 10); $c = random_int(1, 10); $d = random_int(1, 10); echo "<b>7、xuxiaobo_test6({$a}, {$b}, {$c}, {$d}) : </b>\n"; $d = xuxiaobo_test6($a, $b, $c, $d); $e = print_r($d, true);//json_encode($d, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); echo "<pre>".$e."</pre>\n\n"; $a = random_int(1, 10); $b = $arr1[random_int(0, count($arr1)-1)]; echo "<b>8、xuxiaobo_test7({$a}, \"{$b}\", 3.14) : </b>\n"; $d = xuxiaobo_test7($a, $b, 3.14); $e = print_r($d, true);//json_encode($d, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); echo "<pre>".$e."</pre>\n\n";
效果:
1、xuxiaobo_test1() :
已经加载了扩展:test_add2、xuxiaobo_test2() :
你好,世界3、xuxiaobo_test2("工具") :
你好,工具4、xuxiaobo_test3(26, 19) :
455、xuxiaobo_test4("USER") :
root6、xuxiaobo_test5("场景中", "更加", "字符串操作") :
场景中更加字符串操作7、xuxiaobo_test6(4, 9, 10, 2) :
Array ( [0] => 4 [1] => 9 [2] => 10 [3] => 2 [4] => 25 [5] => 720 )8、xuxiaobo_test7(6, "都能", 3.14) :
Array ( [0] => 6 [1] => 都能 [2] => 3.14 [3] => 36 [4] => 都能都能 [5] => 6.28 )
本文来自博客园,作者:河北大学-徐小波,转载请注明原文链接:https://www.cnblogs.com/xuxiaobo/p/18124403