联系客服: iP138.com客服 iP138客服 |

iP查询接口
  • API接口QQ交流群:177096428   iP138 api接口
  • 数据修正QQ处理群:94181690    iP138数据修正

iP查询接口支持HTTPS(赠送1000次)

简介:获取iP地址对应的省市区以及运营商名称

已连接应用数:25106

国内:网宿cdn; 国际:亚洲中国香港、韩国首尔、日本东京、新加坡、欧洲德国法兰克福、北美洲美国硅谷节点

接口地址

http协议:http://api.ip138.com/ipv4/
https协议:https://api.ip138.com/ipv4/

* API接口可能会因为各种网络原因和攻击都可能产生阻断,请开发时做好冗余和异常处理

* 当HTTP请求返回的状态码非200时,请做异常处理,比如 202 状态码造成的原因可能是无效Token、余额不足、格式错误

Object-C(ios)语言调用iP查询接口示例:

                                    //
                                    //  ViewController.m
                                    //  ApiTest
                                    //
                                    //  Created by administrator on 16/9/8.
                                    //  Copyright © 2016年 star. All rights reserved.
                                    //

                                    #import "ViewController.h"

                                    #define Host @"https://api.ip138.com/ipv4/"

                                    @interface ViewController ()<NSXMLParserDelegate>

                                    @end

                                    @implementation ViewController

                                    - (void)viewDidLoad {
                                        [super viewDidLoad];
                                        // Do any additional setup after loading the view, typically from a nib.
                                        NSString * token = @"859476648b3de65d7680494506dd1a1c6a";
                                        
                                        [self executeNetworkWithIP:@"8.8.8.8" dateType:@"xml" callBack:@"" token:token];
                                        
                                    }

                                    /**
                                     *示例https://api.ip138.com/ipv4/?ip=8.8.8.8&datatype=jsonp&callback=&token=859476648b3de65d7680494506dd1a1c6a
                                     *参数说明:
                                     *1. ip string ip地址 例如 117.25.13.123(可选,默认为请求者iP)
                                     *2. datatype string txt|jsonp|xml(可选,默认为jsonp)
                                     *3. callback string 回调函数 当前参数仅为jsonp格式数据提供(可选,默认为空)
                                     *4. token string 购买服务后会提供(必填)
                                     */

                                    - (void)executeNetworkWithIP:(NSString *)ip dateType:(NSString *)dateType callBack:(NSString *)callBack token:(NSString *)token{
                                       
                                        NSString * urlString =[NSString stringWithFormat:@"%@?ip=%@&datatype=%@&callback=%@&token=%@",Host,ip,dateType,callBack,token];
                                        
                                        //准备网络请求
                                        NSString *newStr = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
                                        NSURL *url = [NSURL URLWithString:newStr];
                                        NSURLRequest *requst = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:10];
                                        
                                        //请求异步链接
                                        [NSURLConnection sendAsynchronousRequest:requst queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
                                            
                                            if (connectionError) {
                                                NSLog(@"jsonError = %@",connectionError);
                                                return ;
                                            }
                                            
                                            //请求格式txt
                                            if ([dateType isEqualToString:@"txt"]) {
                                                 NSString *result = [[NSString alloc] initWithData:data  encoding:NSUTF8StringEncoding];
                                                NSLog(@"result = %@",result);
                                            }
                                            //datatype xml
                                            else if ([dateType isEqualToString:@"xml"]){
                                           
                                                NSString *result = [[NSString alloc] initWithData:data  encoding:NSUTF8StringEncoding];
                                                NSLog(@"result = %@",result);
                                            }
                                            else{
                                                NSError *jsonError = nil;
                                                NSString *jsonString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
                                                /*如果设置了回调函数,则可截取后再在转化为NSDictionary,也可直接转化为NSString*/
                                                if (callBack&&[callBack length]>0) {
                                                    NSRange range = [jsonString rangeOfString:@"("];
                                                    range.location++;
                                                    range.length = [jsonString length] - range.location - 1;
                                                    jsonString = [jsonString substringWithRange:range ];
                                                   
                                                }
                                                
                                                NSDictionary *jsonResponse =
                                                [NSJSONSerialization JSONObjectWithData:[jsonString dataUsingEncoding:NSUTF8StringEncoding]
                                                                                options:0
                                                                                  error:&jsonError];
                                                if (jsonError) {
                                                    NSLog(@"jsonError = %@",jsonError);
                                                    return ;
                                                }
                                                NSLog(@"jsonResponse = %@",jsonResponse);
                                            }
                                        }];
                                    }

                                    - (void)didReceiveMemoryWarning {
                                        [super didReceiveMemoryWarning];
                                        // Dispose of any resources that can be recreated.
                                    }

                                    @end