SDK及代码示例:IP地址/域名查询接口API
1、PHP SDK
方法一:以 POST 方式请求数据
//接口参数 $api_url='http://cha.ebaitian.cn/api/json'; $api_appid='1000xxxx'; $api_appkey='56cf61af4b7897e704f67deb88ae8f24'; //函数,以POST方式提交数据,PHP需要开启CURL函数;数据传输安全,建议使用 function getIPInfo($ip){ global $api_url,$api_appid,$api_appkey; $posturl=$api_url; $data='appid='.$api_appid.'&module=getIPAddressInfo&ip='.$ip; $sign=hash("sha256",$data.'&appkey='.$api_appkey); $postdata=array("appid"=>$api_appid,"appkey"=>$api_appkey,"module"=>"getIPAddressInfo","ip"=>$ip,'sign'=>$sign); $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $posturl); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_POSTFIELDS, $postdata); $output = curl_exec($curl); curl_close($curl); $obj=json_decode($output); $result=$obj->result; if($result==1){ $value=$obj->ipInfo->address; if(!empty($obj->ipInfo->isp)){ $value.=$obj->ipInfo->isp; } }else{ $value=$obj->flag; } return $value; } //调用函数 $ip='127.0.0.1'; echo getIPInfo($ip); exit;
方法二:以 GET 方式请求数据
//接口参数 $api_url='http://cha.ebaitian.cn/api/json'; $api_appid='1000xxxx'; $api_appkey='56cf61af4b7897e704f67deb88ae8f24'; //函数,以GET方式提交数据 function getIPInfo($ip){ global $api_url,$api_appid,$api_appkey; $data='appid='.$api_appid.'&module=getIPAddressInfo&ip='.$ip; $sign=hash("sha256",$data.'&appkey='.$api_appkey); $info_get=file_get_contents($api_url.'?type=get&'.$data.'&sign='.$sign); $info_json=json_decode($info_get, true); $result=$info_json['result']; if($result==1){ $value=$info_json['ipInfo']['address']; if(!empty($info_json['ipInfo']['isp'])){ $value.=$info_json['ipInfo']['isp']; } }else{ $value=$info_json['flag']; } return $value; } //调用函数 $ip='127.0.0.1'; echo getIPInfo($ip); exit;
2、Java SDK
//以下示例是以 GET 方式请求数据 public class QueryHelper { public static String apiurl="http://cha.ebaitian.cn/api/json"; public static String appid="1000xxxx"; public static String appkey="56cf61af4b7897e704f67deb88ae8f24"; public static String module="getIPAddressInfo"; public static String getSHA256Str(String str){ MessageDigest messageDigest; String encdeStr = ""; try { messageDigest = MessageDigest.getInstance("SHA-256"); byte[] hash = messageDigest.digest(str.getBytes("UTF-8")); encdeStr = Hex.encodeHexString(hash); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return encdeStr; } public static String get(String urlString) { try { URL url = new URL(urlString); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setConnectTimeout(5 * 1000); conn.setReadTimeout(5 * 1000); conn.setDoInput(true); conn.setDoOutput(true); conn.setUseCaches(false); conn.setInstanceFollowRedirects(false); conn.setRequestMethod("GET"); int responseCode = conn.getResponseCode(); if (responseCode == 200) { StringBuilder builder = new StringBuilder(); BufferedReader br = new BufferedReader( new InputStreamReader(conn.getInputStream(),"utf-8")); for (String s = br.readLine(); s != null; s = br.readLine()) { builder.append(s); } br.close(); return builder.toString(); } } catch (IOException e) { e.printStackTrace(); } return null; } public static String queryIP(String ip){ String sign=getSHA256Str("appid="+appid+"&module="+module+"&ip="+ip+"&appkey="+appkey); String url=apiurl+"?type=get&appid="+appid+"&module="+module+"&ip="+ip+"&sign="+sign; return get(url); } } //使用示例 QueryHelper.queryIP("127.0.0.1");
3、Python SDK
#!/usr/bin/python # -*- coding: utf-8 -*- import httplib2 import hashlib from urllib.parse import urlencode #python3 #from urllib import urlencode #python2 apiurl='http://cha.ebaitian.cn/api/json' appid='1000xxxx' appkey='56cf61af4b7897e704f67deb88ae8f24' module='getIPAddressInfo' ip='127.0.0.1' data='appid='+appid+'&module='+module+'&ip='+ip sign_data=data+'&appkey='+appkey # from Crypto.Cipher import AES # from Crypto.Hash import SHA256 # 256 hash_256 = hashlib.sha256() hash_256.update(sign_data.encode('utf-8')) sign = hash_256.hexdigest() postdata = urlencode({'appid':appid,'module':module,'ip':ip,'sign':sign}) url = apiurl+'?type=get&'+postdata http = httplib2.Http() response, content = http.request(url,'GET') print(content.decode("utf-8"))
4、Node.js SDK
方法一:以 POST 方式请求数据
//以 POST 方式提交 var http = require('http'); var querystring = require('querystring'); //参数设置 var appid = '1000xxxx'; var appkey = '56cf61af4b7897e704f67deb88ae8f24'; var module = 'getIPAddressInfo'; //目标查询IP地址/域名 var ip='127.0.0.1'; //签名,SHA256 不可直接调用;函数参考下载地址:https://github.com/alexweber/jquery.sha256 var sign = SHA256('appid='+appid+'&module='+module+'&ip='+ip+'&appkey='+appkey); //这是需要提交的数据 var post_data = { appid: appid, module: module, ip: ip, sign: sign }; var content = querystring.stringify(post_data); var options = { hostname: 'cha.ebaitian.cn', port: 80, path: '/api/json', method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' } }; var req = http.request(options, function (res) { console.log('STATUS: ' + res.statusCode); console.log('HEADERS: ' + JSON.stringify(res.headers)); res.setEncoding('utf8'); res.on('data', function (chunk) { console.log('BODY: ' + chunk); //JSON.parse(chunk) }); }); req.on('error', function (e) { console.log('problem with request: ' + e.message); }); // write data to request body req.write(content); req.end();
方法二:以 GET 方式请求数据
//以 GET 方式提交 var http = require('http'); var querystring = require('querystring'); //参数设置 var appid = '1000xxxx'; var appkey = '56cf61af4b7897e704f67deb88ae8f24'; var module = 'getIPAddressInfo'; //目标查询IP地址/域名 var ip='127.0.0.1'; //签名,SHA256 不可直接调用;函数参考下载地址:https://github.com/alexweber/jquery.sha256 var sign = SHA256('appid='+appid+'&module='+module+'&ip='+ip+'&appkey='+appkey); //这是需要提交的数据 var data = { appid: appid, module: module, ip: ip, sign: sign }; var content = querystring.stringify(data); var options = { hostname: 'cha.ebaitian.cn', port: 80, path: '/api/json?' + content, method: 'GET' }; var req = http.request(options, function (res) { console.log('STATUS: ' + res.statusCode); console.log('HEADERS: ' + JSON.stringify(res.headers)); res.setEncoding('utf8'); res.on('data', function (chunk) { console.log('BODY: ' + chunk); }); }); req.on('error', function (e) { console.log('problem with request: ' + e.message); }); req.end();
5、C# SDK
using System; using System.Collections.Generic; using System.Web; using System.Net; using System.Text; public class getIPInfo{ public static string getInfo(string appid, string appkey, string module, string ip){ string url = string.Format("http://cha.ebaitian.cn/api/json?type=get&appid={0}&module={1}&ip={2}&sgin={3}", appid, module, ip, sgin); using (WebClient client = new WebClient()){ client.Encoding = Encoding.UTF8; return client.DownloadString(url); } } } string ipInfo = getIPInfo.getInfo("1000xxxx", "getIPAddressInfo", "127.0.0.1", "ecab4881ee80ad3d76bb1da68387428ca752eb885e52621a3129dcf4d9bc4fd4", Request.UserHostAddress); Console.WriteLine(ipInfo); Response.Write(ipInfo);
6、JavaScript SDK
方法一:以 POST 方式请求数据
//使用 JQuery 请先加载最新的 JQuery 插件 //参数设置 var apiurl = 'http://cha.ebaitian.cn/api/json'; var appid = '1000xxxx'; var appkey = '56cf61af4b7897e704f67deb88ae8f24'; var module = 'getIPAddressInfo'; //目标查询IP地址/域名 var ip='127.0.0.1'; //签名,SHA256 不可直接调用;函数参考下载地址:https://github.com/alexweber/jquery.sha256 var sign = SHA256('appid='+appid+'&module='+module+'&ip='+ip+'&appkey='+appkey); //提交数据 $.ajax({ url:apiurl, type:'post', dataType:'json', data:{ appid:appid, module:module, ip:ip, sign:sign }, success:function(res){ console.log(res); } });
方法二:以 GET 方式请求数据
//使用 JQuery 请先加载最新的 JQuery 插件 //参数设置 var apiurl = 'http://cha.ebaitian.cn/api/json'; var appid = '1000xxxx'; var appkey = '56cf61af4b7897e704f67deb88ae8f24'; var module = 'getIPAddressInfo'; //目标查询IP地址/域名 var ip='127.0.0.1'; //签名,SHA256 不可直接调用;函数参考下载地址:https://github.com/alexweber/jquery.sha256 var sign = SHA256('appid='+appid+'&module='+module+'&ip='+ip+'&appkey='+appkey); //提交数据 $.ajax({ url:apiurl, type:'post', dataType:'json', data:{ appid:appid, module:module, ip:ip, sign:sign }, success:function(res){ console.log(res); } });
7、ASP SDK
'设置参数 dim apiurl, appid, appkey, module, ip, sign apiurl="http://cha.ebaitian.cn/api/json" appid="1000xxxx' appkey="56cf61af4b7897e704f67deb88ae8f24" module="getIPAddressInfo" ip="127.0.0.1" '签名,SHA256 不可直接调用;函数参考地址:https://blog.csdn.net/yesoce/article/details/128546 sgin=SHA256("appid=&appid&"&module="&module&"&ip="&ip&"&appkey="&appkey) '异步提交数据 function PostHTTPPage(url,data) dim Http set Http=server.createobject("MSXML2.SERVERXMLHTTP.3.0") Http.open "POST",url,false Http.setRequestHeader "Content-Type", "application/x-www-form-urlencoded" Http.send(data) if Http.readystate<>4 then exit function End if PostHTTPPage=bytesToBSTR(Http.responseBody,"UTF-8") set http=nothing if err.number<>0 then err.Clear End function '提交数据 dim postdata, strTest postdata="appid=&appid&"&module="&module&"&ip="&ip&"&sign="&sign strTest=PostHTTPPage(apiurl,postdata) '返回结果 response.write(strTest) response.end
本文为「本站原创」,未经我们许可,严谨任何人或单位以任何形式转载或刊载本文章,我们保留依法追究侵权的权力!
微信联系我们
使用微信扫一扫
昵称:亿百天技术
公司:星空体育·(StarSky Sports)官方网站
电话:027-88773336
手机:15342213852
邮箱:serviceebaitian.cn
我来说两句