linux c++ curl 根据IP地址获得当前网络的所在的地理位置
注意:
可能每个电脑的默认中文编码格式不同,有时会出现乱码,需要对返回内容进行编码转换,或者换成可指定编码格式的接口。如
搜狐IP地址查询接口(可设置编码):http://pv.sohu.com/cityjson?ie=utf-8
根据IP地址获得当前网络的所在的地理位置
1、几个免费IP地址查询API接口如下:
1. 搜狐IP地址查询接口(默认GBK):http://pv.sohu.com/cityjson
2. 搜狐IP地址查询接口(可设置编码):http://pv.sohu.com/cityjson?ie=utf-8
3. 126 http://ip.ws.126.net/ipquery
5. IP地址查询接口:http://apis.juhe.cn/ip/ip2addr
要先去https://www.juhe.cn/docs/api/id/1申请APPKEY
6. 新浪的IP地址查询接口:http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js
新浪这个应该说是最不错的。并且返回的数据类型为可以自定义格式(默认为纯文本格式,根据format的参数定义,还可以返回JS、Json格式)。
7. 新浪多地域测试方法:
http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js&ip=219.242.98.111
8. 淘宝 http://ip.taobao.com/service/getIpInfo.php?ip=117.89.35.58
参考:
httphttp://blog.csdn.net/ishxiao/article/details/52670242
httphttp http://blog.csdn.net/kong1940742529/article/details/54945924
2、本文采用的是 搜狐IP地址查询接口
http://pv.sohu.com/cityjson?ie=utf-8
返回结果:
var returnCitySN = {"cip": "36.23.67.140", "cid": "330000", "cname": "浙江省"};
在 ubuntu 14.04 c++ 使用 libcurl 库发送请求:
代码如下:
getCityByIp.cpp
#include <stdio.h> #include <curl/curl.h> #include <iostream> #include <jsoncpp/json/json.h> using namespace std; int writer(char *data, size_t size, size_t nmemb, string *writerData) { unsigned long sizes = size * nmemb; if (writerData == NULL) return -1; writerData->append(data, sizes); return sizes; } string parseJsonLocation(string input) { Json::Value root; Json::Reader reader; if("" != input) { input = input.substr((int)input.find("{")); } bool parsingSuccessful = reader.parse(input, root); if(!parsingSuccessful) { std::cout<<"!!! Failed to parse the location data"<< std::endl; return ""; } // Json::StyledWriter sw; // cout << sw.write(root) << endl; //缩进输出 string cip = root["cip"].asString(); string cname = root["cname"].asString(); return cname; } string () { string buffer=""; string location=""; try { CURL *pCurl = NULL; CURLcode res; curl_global_init(CURL_GLOBAL_ALL); // In windows, this will init the winsock stuff string url_str = "http://pv.sohu.com/cityjson?ie=utf-8";//http://ip.ws.126.net/ipquery"; pCurl = curl_easy_init(); // get a curl handle if (NULL != pCurl) { curl_easy_setopt(pCurl, CURLOPT_TIMEOUT, 10); curl_easy_setopt(pCurl, CURLOPT_URL, url_str.c_str()); curl_easy_setopt(pCurl, CURLOPT_WRITEFUNCTION, writer); curl_easy_setopt(pCurl, CURLOPT_WRITEDATA, &buffer); res = curl_easy_perform(pCurl); if (res != CURLE_OK) { printf("curl_easy_perform() failed:%s\n", curl_easy_strerror(res)); } curl_easy_cleanup(pCurl); } curl_global_cleanup(); } catch (std::exception &ex) { printf("curl exception %s.\n", ex.what()); } if(buffer.empty()) { std::cout<< "!!! ERROR The sever response NULL" << std::endl; } else { location = parseJsonLocation(buffer); } return location; } int main(int argc, char const *argv[]) { cout<< getCityByIp(); return 0; }
Makefile
SRC_OBJ=getCityByIp.o
SRC_BIN=getCityByIp
SRC_LIB= -ljsoncpp -lcurl
CC=g++
${SRC_BIN} : ${SRC_OBJ}
${CC} -o $@ $^ ${SRC_LIB}
${RM} ${SRC_OBJ}
clean:
${RM} ${SRC_OBJ} ${SRC_BIN}