C语言,PHP扩展开发,类2

本次实现了7个方法,特点是引入了外部库。

c语言代码:

/* xuxiaobo 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_smart_str.h"
#include "zend_exceptions.h"
#include "zend_globals.h"
#include "php_xuxiaobo.h"
#include "ext/standard/info.h"
#include "mysql/mysql.h"
#pragma comment(lib,"libmysql")
#include <libmemcached/memcached.h>
#include "hiredis/hiredis.h"

//第一个参数表示必传的参数个数,第二个参数表示最多传入的参数个数
//https://blog.csdn.net/qq_21891743/article/details/131386438
//https://www.jianshu.com/p/05616d23c0dc
//https://blog.csdn.net/qq_40647372/article/details/132872085
//Z_PARAM_OPTIONAL 对应(分隔符) | Z_PARAM_STR 对应 S
//Z_PARAM_OPTIONAL 只在有些参数可以不传的时候使用

#ifndef ZEND_PARSE_PARAMETERS_NONE
#define ZEND_PARSE_PARAMETERS_NONE() \
    ZEND_PARSE_PARAMETERS_START(0, 0) \
    ZEND_PARSE_PARAMETERS_END()
#endif


// 自定义排序函数
void bubble_sort(zval *array_arg) {
    int i, j, temp;
    zval *entry_swap1, *entry_swap2;
    int count = zend_hash_num_elements(Z_ARRVAL_P(array_arg));

    for (j = 0; j < count - 1; j++) {
        for (i = 0; i < count - 1 - j; i++) {
            entry_swap1 = zend_hash_index_find(Z_ARRVAL_P(array_arg), i);
            entry_swap2 = zend_hash_index_find(Z_ARRVAL_P(array_arg), i + 1);

            if (Z_LVAL_P(entry_swap1) > Z_LVAL_P(entry_swap2)) {
                temp = Z_LVAL_P(entry_swap1);
                ZVAL_LONG(entry_swap1, Z_LVAL_P(entry_swap2));
                ZVAL_LONG(entry_swap2, temp);
            }
        }
    }
}

// 自定义排序函数
void bubble_sort_cn(zval *array_arg) {
    int i, j;
    zval *entry_swap1, *entry_swap2;
    int count = zend_hash_num_elements(Z_ARRVAL_P(array_arg));

    for (j = 0; j < count - 1; j++) {
        for (i = 0; i < count - 1 - j; i++) {
            entry_swap1 = zend_hash_index_find(Z_ARRVAL_P(array_arg), i);
            entry_swap2 = zend_hash_index_find(Z_ARRVAL_P(array_arg), i + 1);

            char str1[256];
            sprintf(str1, "%s", Z_STRVAL_P(entry_swap1));

            char str2[256];
            sprintf(str2, "%s", Z_STRVAL_P(entry_swap2));

            if (strlen(str1) > strlen(str2)) {
                ZVAL_STRING(entry_swap1, str2);
                ZVAL_STRING(entry_swap2, str1);
            }
        }
    }
}

// 自定义函数
void bubble_get_type(zval *array_arg) {
    int i;
    zval *entry_swap;
    int count = zend_hash_num_elements(Z_ARRVAL_P(array_arg));

    for (i = 0; i < count; i++) {
        entry_swap = zend_hash_index_find(Z_ARRVAL_P(array_arg), i);
        if (Z_TYPE_P(entry_swap) == IS_NULL) {
            ZVAL_STRING(entry_swap, "null");
        }
        else if (Z_TYPE_P(entry_swap) == IS_TRUE || Z_TYPE_P(entry_swap) == IS_FALSE) {
            ZVAL_STRING(entry_swap, "bool");
        }
        else if (Z_TYPE_P(entry_swap) == IS_LONG) {
            ZVAL_STRING(entry_swap, "long");
        }
        else if (Z_TYPE_P(entry_swap) == IS_DOUBLE) {
            ZVAL_STRING(entry_swap, "double");
        }
        else if (Z_TYPE_P(entry_swap) == IS_STRING) {
            ZVAL_STRING(entry_swap, "string");
        }
        else if (Z_TYPE_P(entry_swap) == IS_ARRAY) {
            ZVAL_STRING(entry_swap, "array");
        }
        else if (Z_TYPE_P(entry_swap) == IS_OBJECT) {
            ZVAL_STRING(entry_swap, "object");
        }
        else if (Z_TYPE_P(entry_swap) == IS_RESOURCE) {
            ZVAL_STRING(entry_swap, "resource");
        }
    }
}

PHP_METHOD(Xuxiaobo, extload)
{
    ZEND_PARSE_PARAMETERS_NONE();
    php_printf("扩展%s已经加载并运行", "xuxiaobo");
}

PHP_METHOD(Xuxiaobo, hello)
{
    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_METHOD(Xuxiaobo, hellomust)
{
    char *var;
    size_t var_len;
    zend_string *retval;

    ZEND_PARSE_PARAMETERS_START(1, 1)
        Z_PARAM_STRING(var, var_len)
    ZEND_PARSE_PARAMETERS_END();

    retval = strpprintf(0, "你好,%s", var);

    RETURN_STR(retval);
}

PHP_METHOD(Xuxiaobo, hellomuch)
{
    char *name;
    size_t name_len;
    char *other = "你好";
    size_t other_len = sizeof("你好") - 1;
    zend_string *retval;

    ZEND_PARSE_PARAMETERS_START(1, 2)
        Z_PARAM_STRING(name, name_len)
        Z_PARAM_OPTIONAL
        Z_PARAM_STRING(other, other_len)
    ZEND_PARSE_PARAMETERS_END();

    retval = strpprintf(0, "%s,%s", other, name);

    RETURN_STR(retval);
}

PHP_METHOD(Xuxiaobo, inputints)
{
    zend_long num1, num2, num3, num4;
    zend_string *retval;

    ZEND_PARSE_PARAMETERS_START(4, 4)
        Z_PARAM_LONG(num1)
        Z_PARAM_LONG(num2)
        Z_PARAM_LONG(num3)
        Z_PARAM_LONG(num4)
    ZEND_PARSE_PARAMETERS_END();

    // 输出数字
    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);
}

PHP_METHOD(Xuxiaobo, inputstrs)
{
    char *name1, *name2, *name3, *name4, *name5;
    size_t name_len1, name_len2, name_len3, name_len4, name_len5;
    zend_string *retval;

    ZEND_PARSE_PARAMETERS_START(5, 5)
        Z_PARAM_STRING(name1, name_len1)
        Z_PARAM_STRING(name2, name_len2)
        Z_PARAM_STRING(name3, name_len3)
        Z_PARAM_STRING(name4, name_len4)
        Z_PARAM_STRING(name5, name_len5)
    ZEND_PARSE_PARAMETERS_END();

    // 输出数字
    array_init(return_value);
    add_index_string(return_value, 0, name1);
    add_index_string(return_value, 1, name2);
    add_index_string(return_value, 2, name3);
    add_index_string(return_value, 3, name4);
    add_index_string(return_value, 4, name5);
}

PHP_METHOD(Xuxiaobo, inputarray) {
    zval *array;

    // 检查参数数量和类型
    if (zend_parse_parameters(ZEND_NUM_ARGS(), "a", &array) == FAILURE) {
        RETURN_NULL();
    }

    // 将数组拷贝到返回数组中
    RETVAL_ARR(zend_array_dup(Z_ARRVAL_P(array)));
}

PHP_METHOD(Xuxiaobo, sortarray) {
    zval *array;

    // 检查参数数量和类型
    if (zend_parse_parameters(ZEND_NUM_ARGS(), "a", &array) == FAILURE) {
        RETURN_NULL();
    }

    bubble_sort(array);

    RETURN_ZVAL(array, 1, 0);
}

PHP_METHOD(Xuxiaobo, sortarraycn) {
    zval *array;

    // 检查参数数量和类型
    if (zend_parse_parameters(ZEND_NUM_ARGS(), "a", &array) == FAILURE) {
        RETURN_NULL();
    }

    bubble_sort_cn(array);

    RETURN_ZVAL(array, 1, 0);
}

PHP_METHOD(Xuxiaobo, datatype) {
    zval *array;

    // 检查参数数量和类型
    if (zend_parse_parameters(ZEND_NUM_ARGS(), "a", &array) == FAILURE) {
        RETURN_NULL();
    }

    bubble_get_type(array);

    RETURN_ZVAL(array, 1, 0);
}

//返回数据类型组织
PHP_METHOD(Xuxiaobo, getinfo) {
    zval subarray1;

    array_init(&subarray1);
    add_assoc_long(&subarray1, "id", 1);
    add_assoc_string(&subarray1, "name", "徐小波");
    add_assoc_string(&subarray1, "addtime", "2024-04-12 12:01:02");
    add_assoc_bool(&subarray1, "sex", 1);

    RETVAL_ZVAL(&subarray1, 0, 0);
}

//返回数据类型组织
PHP_METHOD(Xuxiaobo, getlist) {
    zval subarray1;
    array_init(&subarray1);
    add_assoc_long(&subarray1, "id", 1);
    add_assoc_string(&subarray1, "name", "徐小波");
    add_assoc_string(&subarray1, "addtime", "2024-04-12 12:01:02");
    add_assoc_bool(&subarray1, "sex", 1);

    zval subarray2;
    array_init(&subarray2);
    add_assoc_long(&subarray2, "id", 2);
    add_assoc_string(&subarray2, "name", "张光娟");
    add_assoc_string(&subarray2, "addtime", "2023-11-02 09:23:11");
    add_assoc_bool(&subarray2, "sex", 0);

    zval subarray3;
    array_init(&subarray3);
    add_assoc_long(&subarray3, "id", 3);
    add_assoc_string(&subarray3, "name", "徐紫嫣");
    add_assoc_string(&subarray3, "addtime", "2022-03-01 11:34:25");
    add_assoc_bool(&subarray3, "sex", 0);

    array_init(return_value);
    add_index_zval(return_value, 0, &subarray1);
    add_index_zval(return_value, 1, &subarray2);
    add_index_zval(return_value, 2, &subarray3);
}

/*
config.m4
PHP_ADD_LIBRARY_WITH_PATH(mysqlclient, /usr/lib/x86_64-linux-gnu/, XUXIAOBO_SHARED_LIBADD)
PHP_SUBST(XUXIAOBO_SHARED_LIBADD)
PHP_ADD_INCLUDE(/usr/include/mysql)
*/
PHP_METHOD(Xuxiaobo, getstaffinfo) {
    zend_long uid;

    ZEND_PARSE_PARAMETERS_START(1, 1)
        Z_PARAM_LONG(uid)
    ZEND_PARSE_PARAMETERS_END();

    MYSQL *conn;
    MYSQL_RES *res;
    MYSQL_ROW row;

    char* server = "127.0.0.1";//本地连接
    char* user = "root";//
    char* password = "123456";//mysql密码
    char* database = "ceshi1";//数据库名

    conn = mysql_init(conn);
    mysql_real_connect(conn, server, user, password, database, 3306, NULL, 0);
    mysql_set_character_set(conn, "utf8mb4");

    char query[1024];
    sprintf(query, "select id,staffName,staffNo,workEmail,workMobile from staff_baseinfo where id=%ld", uid);

    int t,r;
    array_init(return_value);
    t = mysql_query(conn, query);
    res = mysql_use_result(conn);
    row = mysql_fetch_row(res);

    zval subarray1;

    array_init(&subarray1);
    add_assoc_long(&subarray1, "id", atoi(row[0]));
    add_assoc_string(&subarray1, "staffName", row[1]);
    add_assoc_string(&subarray1, "staffNo", row[2]);
    add_assoc_string(&subarray1, "workEmail", row[3]);
    add_assoc_string(&subarray1, "workMobile", row[4]);

    mysql_free_result(res);
    mysql_close(conn);

    RETVAL_ZVAL(&subarray1, 0, 0);
}

PHP_METHOD(Xuxiaobo, getstafflist) {
    MYSQL *conn;
    MYSQL_RES *res;
    MYSQL_ROW row;

    char* server = "127.0.0.1";//本地连接
    char* user = "root";//
    char* password = "123456";//mysql密码
    char* database = "ceshi1";//数据库名

    conn = mysql_init(conn);
    mysql_real_connect(conn, server, user, password, database, 3306, NULL, 0);
    mysql_set_character_set(conn, "utf8mb4");

    char query[1024];
    sprintf(query, "select id,staffName,staffNo,workEmail,workMobile from staff_baseinfo order by id desc limit %d,%d", 0, 3);

    int t,r;
    array_init(return_value);
    t = mysql_query(conn, query);
    res = mysql_use_result(conn);
    if(res){
        int i = 0;
        while((row = mysql_fetch_row(res)) != NULL){
            zval subarray;
            array_init(&subarray);
            add_assoc_long(&subarray, "id", atoi(row[0]));
            add_assoc_string(&subarray, "staffName", row[1]);
            add_assoc_string(&subarray, "staffNo", row[2]);
            add_assoc_string(&subarray, "workEmail", row[3]);
            add_assoc_string(&subarray, "workMobile", row[4]);
            add_index_zval(return_value, i, &subarray);
            i++;
        }
    }
    mysql_free_result(res);
    mysql_close(conn);
}

PHP_METHOD(Xuxiaobo, memcachedtest) {
    memcached_st *memc = NULL;
    memcached_return rc;
    memcached_server_st *server  = NULL;// = memcached_servers_parse(NULL);

    time_t expiration = 3;
    uint32_t flags = 0;

    char *memcacheIp = "127.0.0.1";
    char memcacheIpArr[256];
    strcpy(memcacheIpArr, memcacheIp);
    int memcachePort1 = 11211;
    int memcachePort2 = 11212;

    char *key = "key2";
    char *value = "ceshi19810909";
    size_t keylen = strlen(key);
    size_t valuelen = strlen(value);

    memc = memcached_create(NULL);
    server = memcached_server_list_append(server, memcacheIpArr, memcachePort1, &rc);
    server = memcached_server_list_append(server, memcacheIpArr, memcachePort2, &rc);

    memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, 1);

    rc = memcached_server_push(memc, server);

    //数据操作
    rc = memcached_set(memc, key, keylen, value, valuelen, expiration, flags);
    if(MEMCACHED_SUCCESS == rc){
        php_printf("Memcache-SET(%s): OK\n", key);
    }
    else{
        php_printf("Memcache-SET(%s): Failed[%s]\n", key, memcached_strerror(memc, rc));
    }

    char *result = memcached_get(memc, key, keylen, &valuelen, &flags, &rc);
    if(MEMCACHED_SUCCESS == rc){
        php_printf("Memcache-GET(%s): %s\n", key, result);
    }
    else{
        php_printf("Memcache-GET(%s): Failed[%s]\n", key, memcached_strerror(memc, rc));
    }

    // 断开连接
    memcached_server_list_free(server);
    memcached_free(memc);
}

PHP_METHOD(Xuxiaobo, redistest) {
    // 创建一个Redis连接上下文
    redisContext *c = redisConnect("127.0.0.1", 6379);
    if (c == NULL) {
        php_printf("Can't allocate redis context\n");
        return ;
    }

    if (c->err) {
        php_printf("Connection error: %s\n", c->errstr);
        return ;
    }

    // 执行PING命令
    redisReply *reply = redisCommand(c, "PING");
    php_printf("Redis-PING: %s\n", reply->str);
    freeReplyObject(reply);

    reply = redisCommand(c, "SET key1 ceshi19820829");
    php_printf("Redis-SET(key1): %s\n", reply->str);
    freeReplyObject(reply);

    reply = redisCommand(c, "GET key1");
    php_printf("Redis-GET(key1): %s\n", reply->str);
    freeReplyObject(reply);

    // 断开连接
    redisFree(c);
}

PHP_RINIT_FUNCTION(xuxiaobo)
{
#if defined(ZTS) && defined(COMPILE_DL_XUXIAOBO)
    ZEND_TSRMLS_CACHE_UPDATE();
#endif

    return SUCCESS;
}

// 请求清理
PHP_RSHUTDOWN_FUNCTION(xuxiaobo) {
    return SUCCESS;
}

// 清理资源
PHP_MSHUTDOWN_FUNCTION(xuxiaobo) {
    return SUCCESS;
}

PHP_MINFO_FUNCTION(xuxiaobo)
{
    php_info_print_table_start();
    php_info_print_table_header(2, "xuxiaobo support", "enabled");
    php_info_print_table_end();
}

const zend_function_entry xuxiaobo_functions[] = {
    PHP_ME(Xuxiaobo, extload, NULL, ZEND_ACC_PUBLIC)
    PHP_ME(Xuxiaobo, hello, NULL, ZEND_ACC_PUBLIC)
    PHP_ME(Xuxiaobo, hellomust, NULL, ZEND_ACC_PUBLIC)
    PHP_ME(Xuxiaobo, hellomuch, NULL, ZEND_ACC_PUBLIC)
    PHP_ME(Xuxiaobo, inputints, NULL, ZEND_ACC_PUBLIC)
    PHP_ME(Xuxiaobo, inputstrs, NULL, ZEND_ACC_PUBLIC)
    PHP_ME(Xuxiaobo, inputarray, NULL, ZEND_ACC_PUBLIC)
    PHP_ME(Xuxiaobo, sortarray, NULL, ZEND_ACC_PUBLIC)
    PHP_ME(Xuxiaobo, sortarraycn, NULL, ZEND_ACC_PUBLIC)
    PHP_ME(Xuxiaobo, datatype, NULL, ZEND_ACC_PUBLIC)
    PHP_ME(Xuxiaobo, getinfo, NULL, ZEND_ACC_PUBLIC)
    PHP_ME(Xuxiaobo, getlist, NULL, ZEND_ACC_PUBLIC)
    PHP_ME(Xuxiaobo, getstaffinfo, NULL, ZEND_ACC_PUBLIC)
    PHP_ME(Xuxiaobo, getstafflist, NULL, ZEND_ACC_PUBLIC)
    PHP_ME(Xuxiaobo, memcachedtest, NULL, ZEND_ACC_PUBLIC)
    PHP_ME(Xuxiaobo, redistest, NULL, ZEND_ACC_PUBLIC)
    {NULL, NULL, NULL}
};

// 注册类或者函数等
PHP_MINIT_FUNCTION(xuxiaobo) {
    zend_class_entry xuxiaobo_ce;
    INIT_CLASS_ENTRY(xuxiaobo_ce, "Xuxiaobo", xuxiaobo_functions);
    zend_register_internal_class(&xuxiaobo_ce TSRMLS_CC);

    return SUCCESS;
}

zend_module_entry xuxiaobo_module_entry = {
    STANDARD_MODULE_HEADER,
    PHP_XUXIAOBO_EXTNAME,
    NULL,            //xuxiaobo_functions
    PHP_MINIT(xuxiaobo),
    NULL, //PHP_MSHUTDOWN(xuxiaobo)
    NULL, //PHP_RINIT(xuxiaobo)
    NULL, //PHP_RSHUTDOWN(xuxiaobo)
    NULL, //PHP_MINFO(xuxiaobo)
    PHP_XUXIAOBO_VERSION,
    STANDARD_MODULE_PROPERTIES
};

#ifdef COMPILE_DL_XUXIAOBO
# ifdef ZTS
ZEND_TSRMLS_CACHE_DEFINE()
# endif
ZEND_GET_MODULE(xuxiaobo)
#endif

 

config.m4 增加以下内容:

PHP_ADD_LIBRARY_WITH_PATH(mysqlclient, /usr/lib/x86_64-linux-gnu/, XUXIAOBO_SHARED_LIBADD)
PHP_ADD_LIBRARY_WITH_PATH(memcached, /usr/lib/x86_64-linux-gnu/, XUXIAOBO_SHARED_LIBADD)
PHP_ADD_LIBRARY_WITH_PATH(hiredis, /usr/local/lib, XUXIAOBO_SHARED_LIBADD)
PHP_SUBST(XUXIAOBO_SHARED_LIBADD)

PHP_ADD_INCLUDE(/usr/include/mysql)
PHP_ADD_INCLUDE(/usr/include/libmemcached)
PHP_ADD_INCLUDE(/usr/local/include/hiredis/)

 

php文件新增内容:

$xobj = new stdClass();
$xobj->a = 1;
$xobj->b = 2;
$fp = fopen("./test_add.php", 'r');
$a = [
    31,
    '你好',
    3.1415926,
    null,
    true,
    ['a' => 1, 'b' => 2],
    $xobj,
    $fp
];
$aa = $a;
$aa[7] = '$fp';
$ainput = json_encode($aa, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
$b = $xxb->datatype($a);
$c = json_encode($b, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); //print_r($b, true);
echo "<b>10、Xuxiaobo::datatype({$ainput}) : </b>\n";
echo "<pre>".$c."</pre>\n\n";

$b = $xxb->getinfo();
$c = json_encode($b, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); //print_r($b, true);
echo "<b>11、Xuxiaobo::getinfo() : </b>\n";
echo "<pre>".$c."</pre>\n\n";

$b = $xxb->getlist();
$c = json_encode($b, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); //print_r($b, true);
echo "<b>12、Xuxiaobo::getlist() : </b>\n";
echo "<pre>".$c."</pre>\n\n";

$b = $xxb->getstaffinfo(1);
$c = print_r($b, true); //json_encode($b, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
echo "<b>13、Xuxiaobo::getstaffinfo(24518) : </b>\n";
echo "<pre>".$c."</pre>\n\n";

$b = $xxb->getstafflist();
$c = print_r($b, true); //json_encode($b, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
echo "<b>14、Xuxiaobo::getstafflist() : </b>\n";
echo "<pre>".$c."</pre>\n\n";

echo "<b>15、Xuxiaobo::memcachedtest() : </b><pre>\n";
$xxb->memcachedtest();
echo "</pre>\n\n";

echo "<b>16、Xuxiaobo::redistest() : </b><pre>\n";
$xxb->redistest();
echo "</pre>\n\n";

 

其中hiredis需要独立编译

make && make intall

 

效果:

10、Xuxiaobo::datatype([31,"你好",3.1415926,null,true,{"a":1,"b":2},{"a":1,"b":2},"$fp"]) :

["long","string","double","null","bool","array","object","resource"]

11、Xuxiaobo::getinfo() :

{"id":1,"name":"徐小波","addtime":"2024-04-12 12:01:02","sex":true}

12、Xuxiaobo::getlist() :

[{"id":1,"name":"徐小波","addtime":"2024-04-12 12:01:02","sex":true},{"id":2,"name":"张光娟","addtime":"2023-11-02 09:23:11","sex":false},{"id":3,"name":"徐紫嫣","addtime":"2022-03-01 11:34:25","sex":false}]

13、Xuxiaobo::getstaffinfo(1) :

Array
(
    [id] => 1
    [staffName] => 哈哈1
    [staffNo] => 1122334455
    [workEmail] => haha1@123456.com
    [workMobile] => 13245512345
)

14、Xuxiaobo::getstafflist() :

Array
(
    [0] => Array
        (
            [id] => 1
            [staffName] => 哈哈1
            [staffNo] => 1122334455
            [workEmail] => haha1@123456.com
            [workMobile] => 13245512345
        )

    [1] => Array
        (
            [id] => 2
            [staffName] => 哈哈2
            [staffNo] => 1122334456
            [workEmail] => haha2@123456.com
            [workMobile] => 13245512346
        )

    [2] => Array
        (
            [id] => 3
            [staffName] => 哈哈3
            [staffNo] => 1122334457
            [workEmail] => haha3@123456.com
            [workMobile] => 13245512347
        )

)

15、Xuxiaobo::memcachedtest() :

Memcache-SET(key2): OK
Memcache-GET(key2): ceshi19810909

16、Xuxiaobo::redistest() :

Redis-PING: PONG
Redis-SET(key1): OK
Redis-GET(key1): ceshi19820829

 

开发过程用到的命令,信息:

root@xuxb-VirtualBox:/home/xuxb/html/php_ext/php-7.3.9/ext/xuxiaobo# /usr/bin/mysql_config
Usage: /usr/bin/mysql_config [OPTIONS]
Compiler: GNU 11.4.0
Options:
        --cflags         [-I/usr/include/mysql ]
        --cxxflags       [-I/usr/include/mysql ]
        --include        [-I/usr/include/mysql]
        --libs           [-L/usr/lib/x86_64-linux-gnu -lmysqlclient -lzstd -lssl -lcrypto -lresolv -lm]
        --libs_r         [-L/usr/lib/x86_64-linux-gnu -lmysqlclient -lzstd -lssl -lcrypto -lresolv -lm]
        --plugindir      [/usr/lib/mysql/plugin]
        --socket         [/var/run/mysqld/mysqld.sock]
        --port           [0]
        --version        [8.0.36]
        --variable=VAR   VAR is one of:
                pkgincludedir [/usr/include/mysql]
                pkglibdir     [/usr/lib/x86_64-linux-gnu]
                plugindir     [/usr/lib/mysql/plugin]


ps -ef |grep php-cgi |awk '{print $2}'|xargs kill -9


cd /home/xuxb/html/php_ext/php-7.3.9/ext/xuxiaobo/
/usr/local/php-7.3.9/bin/phpize
./configure --with-php-config=/usr/local/php-7.3.9/bin/php-config
make
make install
nohup /usr/local/php-7.3.9/bin/php-cgi -b 127.0.0.1:9001 -c /usr/local/php-7.3.9/php.ini &


java:symbol lookup error:undefined symbol:mysql_init.
原因:未将mysql所需的.so库加入到java.library.path中去
解决:执行命令 export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:.so库的相对路径
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/lib/x86_64-linux-gnu/libmysqlclient.so
export LD_RUN_PATH=LD_RUN_PATH:/usr/lib/x86_64-linux-gnu/libmysqlclient.so
export LIBRARY_PATH=$LIBRARY_PATH:/usr/lib/x86_64-linux-gnu/libmysqlclient.so
export C_INCLUDE_PATH=$C_INCLUDE_PATH:/usr/include/mysql
source /root/.bashrc


find  /  -name  "memcached.h"
find  /  -name  "mysql.h"
find  /  -name  "hiredis.h"
/usr/include/mysql/mysql.h

find / -name libmysqlclient.so
find / -name libhiredis.so
/usr/lib/x86_64-linux-gnu/libmysqlclient.so

ldd -r /usr/local/php-7.3.9/lib/php/extensions/no-debug-non-zts-20180731/xuxiaobo.so | grep mysql_init

 

posted @ 2024-04-15 15:53  河北大学-徐小波  阅读(34)  评论(0编辑  收藏  举报