全部产品

API快速入门

本文为您演示,如何通过阿里云OpenAPI Explorer和阿里云SDK等开发者工具调用IP地理位置库 API。下文中API示例代码需要安装阿里云SDK,详细可参考在线版SDK文档。

使用OpenAPI Explorer调用API示例

本示例通过阿里云OpenAPI Explorer,完成查询IPV4地理位置信息(DescribeIpv4Location)的任务。更多有关OpenAPI Explorer的详情,请参见什么是OpenAPI Explorer

IP地理位置库目前提供的API接口:

  1. 通过 DescribeGeoipInstances 根据传入参数获取实例列表。

  2. 通过 DescribeGeoipInstance 根据传入参数获取实例详情。

  3. 通过 DescribeGeoipInstanceDataInfos 根据传入参数获取离线版实例数据信息。

  4. 通过 DescribeGeoipInstanceDataUrl 根据传入参数获取离线版实例数据下载链接。

  5. 通过 DescribeIpv4Location 根据传入参数查询IPv4地理位置信息。

  6. 通过 DescribeIpv6Location 根据传入参数查询IPv6地理位置信息。

  7. 通过 DescribeGeoipInstanceStatistics 根据传入参数获取接口调用量统计数据。

DescribeIpv4Location的调用测试如图:

使用Java调用API示例

本示例通过Alibaba Cloud SDK for Java,完成查询IPv4地理位置信息(DescribeIpv4Location)的任务。关于如何配置IP地理位置库 SDK,请参见在线版SDK文档。

参数解释:

cn-hangzhou:区域ID,默认且当前仅支持区域为cn-hangzhou。若填写其他区域ID会返回异常。

accessKeyId、accessSecret:AccessKey ID和AccessKey Secret是您访问阿里云API的密钥,具有该账户完全的权限。关于AccessKey的获取。

sdk包引入参考:pom.xml

<repositories>
        <repository>
            <id>sonatype-nexus-staging</id>
            <name>Sonatype Nexus Staging</name>
            <url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
</repositories>
<dependency>    
    <groupId>com.aliyun</groupId>    
    <artifactId>aliyun-java-sdk-geoip</artifactId>    
    <version>1.0.4</version>
</dependency>
<dependency>
    <groupId>com.aliyun</groupId>
    <artifactId>aliyun-java-sdk-core</artifactId>
    <version>4.5.3</version>
</dependency>

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;
import com.google.gson.Gson;
import java.util.*;
import com.aliyuncs.geoip.model.v20200101.*;

public class DescribeIpv4Location {

    public static void main(String[] args) {
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "<accessKeyId>", "<accessSecret>");
        IAcsClient client = new DefaultAcsClient(profile);

        DescribeIpv4LocationRequest request = new DescribeIpv4LocationRequest();
        request.setIp("221.206.131.10");

        try {
            DescribeIpv4LocationResponse response = client.getAcsResponse(request);
            System.out.println(new Gson().toJson(response));
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            System.out.println("ErrCode:" + e.getErrCode());
            System.out.println("ErrMsg:" + e.getErrMsg());
            System.out.println("RequestId:" + e.getRequestId());
        }
    }

}

使用Go调用API示例

本示例通过Alibaba Cloud SDK for Go,完成查询IPv4地理位置信息(DescribeIpv4Location)的任务。

参数解释:

cn-hangzhou:区域ID,默认且当前仅支持区域为cn-hangzhou。若填写其他区域ID会返回异常。

accessKeyId、accessSecret:AccessKey ID和AccessKey Secret是您访问阿里云API的密钥,具有该账户完全的权限。关于AccessKey的获取。

sdk包引入参考:

go get github.com/aliyun/alibaba-cloud-sdk-go/services/geoip

package main

import (
        "fmt"
        "github.com/aliyun/alibaba-cloud-sdk-go/services/geoip"
)

func main() {
        client, err := geoip.NewClientWithAccessKey("cn-hangzhou", "<accessKeyId>", "<accessSecret>")

        request := geoip.CreateDescribeIpv4LocationRequest()
        request.Scheme = "https"
        request.Ip = "221.206.131.10"

        response, err := client.DescribeIpv4Location(request)
        if err != nil {
                fmt.Print(err.Error())
        }
        fmt.Printf("response is %#v\n", response)
}
                                

使用PHP调用API示例

本示例通过Alibaba Cloud SDK for PHP,完成查询IPv4地理位置信息(DescribeIpv4Location)的任务。

参数解释:

cn-hangzhou:区域ID,默认且当前仅支持区域为cn-hangzhou。若填写其他区域ID会返回异常。

accessKeyId、accessSecret:AccessKey ID和AccessKey Secret是您访问阿里云API的密钥,具有该账户完全的权限。关于AccessKey的获取。

sdk包引入参考:

composer require alibabacloud/geoip

<?php
use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;

// Download:https://github.com/aliyun/openapi-sdk-php
// Usage:https://github.com/aliyun/openapi-sdk-php/blob/master/README.md

AlibabaCloud::accessKeyClient('<accessKeyId>', '<accessSecret>')
                        ->regionId('cn-hangzhou')
                        ->asDefaultClient();

try {
    $result = AlibabaCloud::rpc()
                          ->product('geoip')
                          // ->scheme('https') // https | http
                          ->version('2020-01-01')
                          ->action('DescribeIpv4Location')
                          ->method('POST')
                          ->host('geoip.aliyuncs.com')
                          ->options([
                                        'query' => [
                                          'Ip' => "221.206.131.10",
                                        ],
                                    ])
                          ->request();
    print_r($result->toArray());
} catch (ClientException $e) {
    echo $e->getErrorMessage() . PHP_EOL;
} catch (ServerException $e) {
    echo $e->getErrorMessage() . PHP_EOL;
}
                                

使用Python调用API示例

本示例通过Alibaba Cloud SDK for Python,完成查询IPv4地理位置信息(DescribeIpv4Location)的任务。

参数解释:

cn-hangzhou:区域ID,默认且当前仅支持区域为cn-hangzhou。若填写其他区域ID会返回异常。

accessKeyId、accessSecret:AccessKey ID和AccessKey Secret是您访问阿里云API的密钥,具有该账户完全的权限。关于AccessKey的获取。

sdk包引入参考:

pip install aliyun-python-sdk-core
pip install aliyun-python-sdk-geoip

#!/usr/bin/env python
#coding=utf-8

from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.acs_exception.exceptions import ClientException
from aliyunsdkcore.acs_exception.exceptions import ServerException
from aliyunsdkgeoip.request.v20200101.DescribeIpv4LocationRequest import DescribeIpv4LocationRequest

client = AcsClient('<accessKeyId>', '<accessSecret>', 'cn-hangzhou')

request = DescribeIpv4LocationRequest()
request.set_accept_format('json')
request.set_Ip("221.206.131.10")

response = client.do_action_with_exception(request)
# python2:  print(response) 
print(str(response, encoding='utf-8'))