nafisc/ipstackgeo-php

用于与IPStack Geo API接口的PHP库

v1.7.0 2021-12-30 02:46 UTC

This package is auto-updated.

Last update: 2024-09-04 15:33:32 UTC


README

IPStack for PHP是一个用于与IPStack Geo API接口的简单库。

$ composer require nafisc/ipstackgeo-php

Sponsor Me! Maintainability StyleCI CircleCI Coverage Status Latest Stable Version License

了解更多关于IPStack的信息:[ipstack.net](https://ipstack.com/product)(外部链接)

寻找Python版本?

寻找Node.JS版本?

特性

  • 检索任何IP地址的地理位置数据。
  • 检索执行此代码的系统的地理位置数据。
  • 检索客户端的地理位置数据。
  • 检索一批IP地址的地理位置数据。
  • 评估IP地址的安全性。

旧版特性

  • 链接到自定义FreeGeoIP服务器

基本用法

$geo = new GeoLookup('.....');
$location = $geo->getLocation('github.com');
print_r($location);

示例用法

注意:请参阅IPStack: 响应对象以获取响应对象中可用属性列表。

创建GeoLookup对象

use IPStack\PHP\GeoLookup;

// Create the GeoLookup object using your API key.
$geoLookup = new GeoLookup('acecac3893c90871c3');

查找IP地址的位置

// Lookup a location for an IP Address
// and catch any exceptions that might
// be thrown by Guzzle or IPStack.
try {
    // Retrieve the location information for 
    // github.com by using it's hostname.
    // 
    // This function will work with hostnames
    // or IP addresses.
    $location = $geoLookup->getLocation('github.com');

    // If we are unable to retrieve the location information
    // for an IP address, null will be returned.
    if (\is_null($location)) {
        echo 'Failed to find location.'.PHP_EOL;
    } else {
        // Print the Location Object.
        print_r($location);
    }
} catch (\Exception $e) {
    echo $e->getMessage();
}

查找客户端的位置

$location = $geoLookup->getClientLocation();
print_r($location);

查找自身位置

$location = $geoLookup->getOwnLocation();
print_r($location);

其他特性

此外,此库和IPStack API还内置了一些其他有用的特性。

  1. 批量位置查找

    ipstack API还提供了一次性请求多个IPv4或IPv6地址数据的能力。这需要PROFESSIONAL级别或更高版本的API密钥,并且每次限制为50个IP地址。

    查看:[https://ipstack.com/documentation#bulk](https://ipstack.com/documentation#bulk)(外部链接)

    $lookup = ['google.com', 'github.com', '1.1.1.1'];
    $locations = $geoLookup->getLocations(...$lookup);
    print_r($locations);
  2. 请求IP地址的主机名。

    默认情况下,ipstack API不会返回关于给定IP地址解析到的主机名的信息。要包含主机名,请使用以下方法。

    查看:[https://ipstack.com/documentation#hostname](https://ipstack.com/documentation#hostname)(外部链接)

    $location = $geoLookup->setFindHostname(true)->getLocation('1.1.1.1');
    echo $location['hostname'];
    one.one.one.one
    
  3. 评估安全性

    订阅Professional Plus计划的客户可以访问ipstack API的安全模块,该模块可用于在网站或Web应用程序受到损害之前评估来自某些IP地址的风险和威胁。

    查看:[https://ipstack.com/documentation#security](https://ipstack.com/documentation#security)(外部链接)

    $location = $geoLookup->assessSecurity(true)->getLocation('github.com');
  4. 设置响应的语言

    ipstack API能够以不同的语言提供结果集。要请求除英语(默认)以外的语言的数据,请使用以下方法并指定支持的编程语言代码之一。

    查看:[https://ipstack.com/documentation#language](https://ipstack.com/documentation#language)(外部链接)

    支持的语言

    $location = $geoLookup->setLanguage('en')->getLocation('github.com');
  5. 配置您的请求

    /// Use HTTPS
    /// This requires IPStack Basic plan or higher.
    $location = $geoLookup->useHttps(true)->getLocation('github.com');
    
    /// Configure the timeout for requests
    $location = $geoLookup->setTimeout(15)->getLocation('github.com');

使用旧版FreeGeoIP二进制文件

您仍然可以使用托管在服务器上的旧版FreeGeoIP二进制文件。

注意:[FreeGeoIP已被弃用](https://github.com/apilayer/freegeoip/#freegeoip---important-announcement)。

use IPStack\PHP\Legacy\FreeGeoIp as GeoLookup;

// Address, Port, Protocol, Timeout
$geoLookup = new GeoLookup(
    'localhost', // Address hosting the legacy FreeGeoIP Binary
    8080,        // Port that the binary is running on (defaults to 8080)
    'http',      // Protocol to use (defaults to http)
    10           // Timeout (defaults to 10 seconds)
);