20250423
This commit is contained in:
19
libcurl.c
19
libcurl.c
@@ -1,6 +1,5 @@
|
||||
#include "libcurl.h"
|
||||
|
||||
|
||||
struct MemoryStruct {
|
||||
char *memory;
|
||||
size_t size;
|
||||
@@ -28,7 +27,7 @@ static size_t WriteMemoryCallback(void *contents, size_t size, size_t nmemb, voi
|
||||
}
|
||||
|
||||
char *GetLocalAddr(char *url)
|
||||
{
|
||||
{
|
||||
CURL *curl_handle;
|
||||
CURLcode res;
|
||||
|
||||
@@ -100,8 +99,7 @@ char *CurlGetIpArea(char *ip)
|
||||
{
|
||||
char url[256] = { 0 };
|
||||
snprintf(url, sizeof(url), "https://qifu.baidu.com/ip/geo/v1/district?ip=%s", ip);
|
||||
|
||||
|
||||
|
||||
CURL *curl_handle;
|
||||
CURLcode res;
|
||||
|
||||
@@ -169,13 +167,14 @@ char *CurlGetIpArea(char *ip)
|
||||
}
|
||||
|
||||
// 函数用于从 cJSON 对象提取字段内容
|
||||
void parse_string_field(cJSON *parent, const char *field_name, char *output, size_t max_len) {
|
||||
void parse_string_field(cJSON *parent, const char *field_name, char *output, size_t max_len)
|
||||
{
|
||||
cJSON *item = cJSON_GetObjectItemCaseSensitive(parent, field_name);
|
||||
if (cJSON_IsString(item) && item->valuestring) {
|
||||
strncpy(output, item->valuestring, max_len - 1);
|
||||
output[max_len - 1] = '\0'; // 确保字符串以 '\0' 结尾
|
||||
} else {
|
||||
output[0] = '\0'; // 如果字段不存在或不是字符串,设置为空字符串
|
||||
output[0] = '\0'; // 如果字段不存在或不是字符串,设置为空字符串
|
||||
}
|
||||
}
|
||||
|
||||
@@ -186,7 +185,6 @@ int parse_json_to_struct(const char *json_string, Response *response)
|
||||
fprintf(stderr, "Invalid input parameters!\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
// 初始化结构体
|
||||
memset(response, 0, sizeof(Response));
|
||||
|
||||
@@ -196,7 +194,6 @@ int parse_json_to_struct(const char *json_string, Response *response)
|
||||
fprintf(stderr, "Error parsing JSON: %s\n", cJSON_GetErrorPtr());
|
||||
return -1;
|
||||
}
|
||||
|
||||
// 解析字段
|
||||
parse_string_field(root, "code", response->code, sizeof(response->code));
|
||||
parse_string_field(root, "ip", response->ip, sizeof(response->ip));
|
||||
@@ -207,7 +204,6 @@ int parse_json_to_struct(const char *json_string, Response *response)
|
||||
cJSON_Delete(root);
|
||||
return -1;
|
||||
}
|
||||
|
||||
// 解析 data 对象
|
||||
cJSON *data_item = cJSON_GetObjectItemCaseSensitive(root, "data");
|
||||
if (cJSON_IsObject(data_item)) {
|
||||
@@ -222,20 +218,17 @@ int parse_json_to_struct(const char *json_string, Response *response)
|
||||
parse_string_field(data_item, "district", response->data.district, sizeof(response->data.district));
|
||||
parse_string_field(data_item, "region", response->data.region, sizeof(response->data.region));
|
||||
}
|
||||
|
||||
// ----------- 拼接 continent_country ---------------
|
||||
if (snprintf(response->continent_country, sizeof(response->continent_country), "%s%s", response->data.continent, response->data.country) >= sizeof(response->continent_country)) {
|
||||
fprintf(stderr, "continent_country truncated!\n");
|
||||
}
|
||||
|
||||
//snprintf(response->continent_country, 256, "%s%s", response->data.continent, response->data.country);
|
||||
|
||||
|
||||
// 清理 JSON 对象
|
||||
cJSON_Delete(root);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user