当前位置: 首页 > wzjs >正文

wordpress-akina网站seo运营培训机构

wordpress-akina,网站seo运营培训机构,西安做网站公司报价,eclipse开发网站用vue做前端本文使用curl获取ip地理位置的json格式,并通过cjson进行解析,使用curl的三种方式进行请求: 命令行system()将curl后的内容拷贝到文件中,后进行解析 使用libcurl请求 使用libcurl multi 异步方式请求 1.获取ip地理位置的api 常…

本文使用curl获取ip地理位置的json格式,并通过cjson进行解析,使用curl的三种方式进行请求:

  • 命令行system()将curl后的内容拷贝到文件中,后进行解析

  • 使用libcurl请求

  • 使用libcurl multi 异步方式请求

1.获取ip地理位置的api

常用的获取ip地理位置的api:

  • ip-api.com 接口,具体使用方式查看官网
http://ip-api.com/json/{query}
  • whois.pconline.com.cn 接口
http://whois.pconline.com.cn/ipJson.jsp

本文使用ip_api.com接口,可在网页上进行demo测试,网址如下:

https://ip-api.com/docs/api:json

在这里插入图片描述

可以在returned date勾选需要返回的值

在这里插入图片描述

例如,需获取当前ip的countryCode,regionName,city,lat,lon,则勾线相应的选项

在这里插入图片描述

点击demo按键,则会跳转到测试回复这里,则会显示当前回复的数据内容

在这里插入图片描述

2. 使用cjson进行解析

  • cJson:一个基于 C 语言的 Json 库,它是一个开源项目,github 下载地址:cjosn github 地址
  • cJson库组成:主要的文件有两个,一个 cJSON.c 一个 cJSON.h。使用时,将头文件 include 进去即可

本文不对cjson进行详细说明

首先将json串进行压缩转义,可使用的网址为:压缩转义json网页,将上述返回值转义为字符串,方便测试,例如:

{\"status\":\"success\",\"country\":\"Canada\",\"countryCode\":\"CA\",\"regionName\":\"Quebec\",\"city\":\"Montreal\",\"lat\":45.6026,\"lon\":-73.5167}

根据ip_api的返回值json,可进行相关解析,解析如下:

#include <stdio.h>
#include <string.h>
#include "cJSON.h"typedef struct ip_info {char countryCode[8];char regionName[32];char city[32];double lat;double lon;
} ip_info;ip_info current_ip_info = {0};int parse_ip_json_str(const char *current_json_str){int ret = -1;cJSON *tmp_json = cJSON_Parse(current_json_str);if (!tmp_json ) {const char *error_ptr = cJSON_GetErrorPtr();if (error_ptr) fprintf(stderr, "JSON解析错误: %s\n", error_ptr);return -1;}cJSON *status = cJSON_GetObjectItemCaseSensitive(tmp_json , "status");if (!cJSON_IsString(status) || strcmp(status->valuestring, "success") != 0) {ret = -1;goto end;}cJSON *item;if ((item = cJSON_GetObjectItem(tmp_json , "countryCode")) && cJSON_IsString(item)) strncpy(current_ip_info.countryCode, item->valuestring, sizeof(current_ip_info.countryCode)-1);if ((item = cJSON_GetObjectItem(tmp_json , "regionName")) && cJSON_IsString(item)) strncpy(current_ip_info.regionName, item->valuestring, sizeof(current_ip_info.regionName)-1);if ((item = cJSON_GetObjectItem(tmp_json , "city")) && cJSON_IsString(item)) strncpy(current_ip_info.city, item->valuestring, sizeof(current_ip_info.city)-1);if ((item = cJSON_GetObjectItem(tmp_json , "lat")) && cJSON_IsNumber(item)) current_ip_info.lat = item->valuedouble;if ((item = cJSON_GetObjectItem(tmp_json , "lon")) && cJSON_IsNumber(item)) current_ip_info.lon = item->valuedouble;if(strlen(current_ip_info.countryCode) != 0 && strlen(current_ip_info.regionName) != 0 && strlen(current_ip_info.city) != 0 && current_ip_info.lat != 0 && current_ip_info.lon != 0) ret = 0;
end:cJSON_Delete(tmp_json);return ret;
}void print_ip_json_parse(void) {printf("current ip info:\n");printf("countryCode ID: %s\n", current_ip_info.countryCode);printf("regionName: %s\n", current_ip_info.regionName);printf("city: %s\n", current_ip_info.city);printf("lat: %.4f\n", current_ip_info.lat);printf("lon: %.4f\n", current_ip_info.lon);printf("\n");
}int main() {const char json_str[] = ""{\"status\":\"success\",\"country\":\"Canada\",\"countryCode\":\"CA\",\"regionName\":\"Quebec\",\"city\":\"Montreal\",\"lat\":45.6026,\"lon\":-73.5167};if(parse_ip_json_str(json_str) == 0) {print_ip_json_parse();}
}

则打印结果为:

在这里插入图片描述

3. 使用curl获取api返回值

3.1 使用system指令直接获取

使用system(curl ....) 是通过调用系统的命令行来运行 curl 命令。这意味着它会启动一个外部进程来执行 curl,并在终端或命令行环境中运行该命令。

缺点

  • 性能开销:每次调用 system(curl) 都需要启动一个新的外部进程,这会增加系统开销,影响性能;
  • 且若无系统调度,则会阻塞主线程的运行;
  • 依赖于系统的 curl 命令:你必须依赖外部 curl 工具,无法灵活控制;
  • 输出不方便处理:调用 system(curl) 会将结果输出到标准输出,处理结果需要额外的工作;
  • 安全性:如果拼接命令字符串时不小心,可能会导致安全漏洞,尤其是在传递不受信任的输入时;

则该方式适合测试时使用,或者调用次数及性能要求不高的场景需求,不推荐在正式代码中使用,本文采用静默且将输出保存到文件中,封装的函数如下;

void execute_curl(const char *url, const char *output_file) {char command[256] = {0};if (output_file) {snprintf(command, sizeof(command), "curl -s -o '%s' '%s'", output_file, url);} else {snprintf(command, sizeof(command), "curl -s '%s'", url);}system(command);
}

则使用其进行curl请求

#define TEMP_FILE_PATH "/tmp/tmp_file.json"
#define CURL_IP_API     "http://ip-api.com/json/?fields=status,message,countryCode,regionName,city,lat,lon"execute_curl(CURL_IP_API, TEMP_FILE_PATH);

使用cat指令查看,确定是保存成功的,如下

在这里插入图片描述

之后就可以将其从文件中读出,删除tmp文件,解析json数据,下面为将其从文件读出,并返回json数据的代码:

cJSON *parse_json_file(const char *filename) {FILE *fp = fopen(filename, "rb");  // 以二进制模式打开确保ftell正确if (!fp) return NULL;// 获取文件大小fseek(fp, 0, SEEK_END);long file_size = ftell(fp);fseek(fp, 0, SEEK_SET);// 处理空文件情况if (file_size <= 0) {fclose(fp);return NULL;}// 动态分配缓冲区char *buffer = (char*)malloc(file_size + 1);if (!buffer) {fclose(fp);return NULL;}// 读取文件内容size_t bytes_read = fread(buffer, 1, file_size, fp);fclose(fp);if (bytes_read != (size_t)file_size) {free(buffer);return NULL;}// 添加字符串终止符buffer[bytes_read] = '\0';// 解析JSONcJSON *json = cJSON_Parse(buffer);free(buffer);  // 释放原始缓冲区return json;
}

可根据需求添加是否要删除临时文件

unlink(TEMP_FILE_PATH);

则将其整合起来的代码如下:

#include <stdio.h>
#include <string.h>
#include "cJSON.h"
#include <<unistd.h>#define TEMP_FILE_PATH "/tmp/tmp_file.json"
#define CURL_IP_API     "http://ip-api.com/json/?fields=status,message,countryCode,regionName,city,lat,lon"typedef struct ip_info {char countryCode[8];char regionName[32];char city[32];double lat;double lon;
} ip_info;ip_info current_ip_info = {0};int parse_ip_json_str(cJSON *current_json_str){int ret = -1;cJSON *status = cJSON_GetObjectItemCaseSensitive(current_json_str, "status");if (!cJSON_IsString(status) || strcmp(status->valuestring, "success") != 0) {ret = -1;goto end;}cJSON *item;if ((item = cJSON_GetObjectItem(current_json_str, "countryCode")) && cJSON_IsString(item)) strncpy(current_ip_info.countryCode, item->valuestring, sizeof(current_ip_info.countryCode)-1);if ((item = cJSON_GetObjectItem(current_json_str, "regionName")) && cJSON_IsString(item)) strncpy(current_ip_info.regionName, item->valuestring, sizeof(current_ip_info.regionName)-1);if ((item = cJSON_GetObjectItem(current_json_str, "city")) && cJSON_IsString(item)) strncpy(current_ip_info.city, item->valuestring, sizeof(current_ip_info.city)-1);if ((item = cJSON_GetObjectItem(current_json_str, "lat")) && cJSON_IsNumber(item)) current_ip_info.lat = item->valuedouble;if ((item = cJSON_GetObjectItem(current_json_str, "lon")) && cJSON_IsNumber(item)) current_ip_info.lon = item->valuedouble;if(strlen(current_ip_info.countryCode) != 0 && strlen(current_ip_info.regionName) != 0 && strlen(current_ip_info.city) != 0 && current_ip_info.lat != 0 && current_ip_info.lon != 0) ret = 0;
end:return ret;
}void print_ip_json_parse(void) {printf("current ip info:\n");printf("countryCode ID: %s\n", current_ip_info.countryCode);printf("regionName: %s\n", current_ip_info.regionName);printf("city: %s\n", current_ip_info.city);printf("lat: %.4f\n", current_ip_info.lat);printf("lon: %.4f\n", current_ip_info.lon);printf("\n");
}cJSON *parse_json_file(const char *filename) {FILE *fp = fopen(filename, "rb");  // 以二进制模式打开确保ftell正确if (!fp) return NULL;// 获取文件大小fseek(fp, 0, SEEK_END);long file_size = ftell(fp);fseek(fp, 0, SEEK_SET);// 处理空文件情况if (file_size <= 0) {fclose(fp);return NULL;}// 动态分配缓冲区char *buffer = (char*)malloc(file_size + 1);if (!buffer) {fclose(fp);return NULL;}// 读取文件内容size_t bytes_read = fread(buffer, 1, file_size, fp);fclose(fp);if (bytes_read != (size_t)file_size) {free(buffer);return NULL;}// 添加字符串终止符buffer[bytes_read] = '\0';// 解析JSONcJSON *json = cJSON_Parse(buffer);free(buffer);  // 释放原始缓冲区return json;
}int main() {execute_curl(CURL_IP_API, TEMP_FILE_PATH);cJSON *root = parse_json_file(TEMP_FILE_PATH);if (!root) {printf("parse_json_file error\r\n");unlink(TEMP_FILE_PATH)return -1;}if(parse_ip_json_str(root) == 0) {print_ip_json_parse();}cJSON_Delete(root);unlink(TEMP_FILE_PATH);return 0;
}

使用libcurl请求和使用libcurl multi 异步方式请看下篇,如有错误,请指正,谢谢~

http://www.dtcms.com/wzjs/458204.html

相关文章:

  • 商业网站是什么seo具体怎么优化
  • html5 手机网站开发叫才网络公关
  • 免费网站建设空间深圳seo推广公司
  • 宝塔怎么做两个网站的解析百度指数查询官方网
  • 自己做网站的难度手机网站排名优化
  • 成都有实力的网站建设太原seo排名外包
  • 软件开发的流程是什么学seo需要多久
  • 网站dede后台免费关键词优化排名软件
  • 桂林旅游网官方网站网站一级域名和二级域名区别
  • 做棋牌网站合法下载百度安装
  • 高大上的公司网站免费发布信息网平台
  • 语言网站开发互联网营销的优势
  • 扁平式网站模板北京seo关键词排名优化
  • 装修工人找活的接单平台湖北搜索引擎优化
  • 实用网站建设营销推广主要包括
  • 百度上网站怎么做百度指数移动版怎么用
  • 常熟祥云平台网站建设网站推广应该怎么做?
  • 个人做民宿需要建立网站吗谷歌优化培训
  • 重新建设网站的请示2021全国大学生营销大赛
  • 网站数据分析报表搜索引擎营销seo
  • 拖拽式网站鸡西网站seo
  • 郑州手机网站建设公司一个万能的营销方案
  • 网站制作电话天津百度
  • 网站域名备案多长时间长春关键词优化排名
  • 我做彩票网站开发彩票网站搭建什么叫seo优化
  • 网站建设和推广的话术今日头条新闻头条
  • 手机怎样设计网站建设网页制作学习
  • 如何向百度举报网站四川二级站seo整站优化排名
  • 外贸建站网站建设个人网页制作成品
  • 深圳优秀网站建设公司上海百度分公司电话