ipinfo.io服务的包装器

v0.7.0 2022-12-20 19:17 UTC

This package is auto-updated.

Last update: 2024-09-20 22:58:35 UTC


README

Latest version Build Status Coverage Status Quality Score Total Downloads PSR2 Conformance

ipinfo.io服务的包装器。

安装

您可以使用 composer 安装此库

$ composer require davidepastore/ipinfo

如何使用

设置

令牌

您可以在实例化对象时设置您的令牌,但这不是强制的。

$ipInfo = new DavidePastore\Ipinfo\Ipinfo(array(
	"token" => "your_api_key"
));

cURL选项

实例化对象时尝试连接时要使用的cURL选项

$ipInfo = new DavidePastore\Ipinfo\Ipinfo(array(
	"curlOptions" => array(
            CURLOPT_CONNECTTIMEOUT => 1,
            CURLOPT_TIMEOUT => 2,
            CURLOPT_CAINFO => __DIR__ . "/cacert.pem"
    )
));

读取给定IP的详细信息

您可以读取给定IP的所有属性。

//Get all the properties
$host = $ipInfo->getFullIpDetails("8.8.8.8");

//Get only a single property (this could save bandwidth)
$city = $ipInfo->getSpecificField("8.8.8.8", DavidePastore\Ipinfo\Ipinfo::CITY);

读取您自己的IP的详细信息

您可以读取您自己的IP的所有属性。

//Get all the properties
$host = $ipInfo->getYourOwnIpDetails();

//Get only a single property (this could save bandwidth)
$city = $ipInfo->getYourOwnIpSpecificField(DavidePastore\Ipinfo\Ipinfo::CITY);

从主机获取信息

在获得Host实例后,您可以读取所有属性或逐个读取。

//Read all the properties
$city = $host->getCity();
$country = $host->getCountry();
$hostname = $host->getHostname();
$ip = $host->getIp();
$loc = $host->getLoc();
$org = $host->getOrg();
$phone = $host->getPhone();
$postal = $host->getPostal();
$region = $host->getRegion();

//Get the associative array with all the properties
$properties = $host->getProperties();

只读取一个字段

有一些不同的常量,您可以使用这些常量通过getSpecificField()getYourOwnIpSpecificField()方法从Ipinfo实例中读取特定字段的值。

IpInfo::IP; //For the ip address
IpInfo::HOSTNAME; //For the hostname
IpInfo::LOC; //For the loc
IpInfo::ORG; //For the org
IpInfo::CITY; //For the city
IpInfo::REGION; //For the region
IpInfo::COUNTRY; //For the country
IpInfo::PHONE; //For the phone
IpInfo::POSTAL; //For the postal
IpInfo::GEO; //For the geo info. See the paragraph below for more info

只读取Geo数据(更快)

使用getIpGeoDetails()方法,您将获得较少的字段。这个调用通常比getFullIpDetails()更快,因此如果只需要以下字段,请使用此调用

IpInfo::IP; //For the ip address
IpInfo::CITY; //For the city
IpInfo::REGION; //For the region
IpInfo::COUNTRY; //For the country
IpInfo::PHONE; //For the phone
IpInfo::POSTAL; //For the postal

这些字段将为空

IpInfo::HOSTNAME; //For the hostname
IpInfo::LOC; //For the loc
IpInfo::ORG; //For the org

错误处理

您可以通过捕获IpInfoExceptionException来处理所有类型的IpInfo异常。

use DavidePastore\Ipinfo\Exception\IpInfoExceptionException;

try {
    $host = $ipInfo->getFullIpDetails("8.8.8.8");
} catch (IpInfoExceptionException $e) {
    echo 'Caught exception: ',  $e->getMessage(), "\n";
}

无效令牌异常

使用您用于API调用的令牌可能无效。您可以通过捕获InvalidTokenException来处理它。

use DavidePastore\Ipinfo\Exception\InvalidTokenException;

try {
    $host = $ipInfo->getFullIpDetails("8.8.8.8");
} catch (InvalidTokenException $e) {
    echo 'Caught exception: ',  $e->getMessage(), "\n";
}

超出速率限制异常

可能发生您API调用超出速率限制的情况。您可以通过捕获RateLimitExceedException来处理它。

use DavidePastore\Ipinfo\Exception\RateLimitExceedException;

try {
    $host = $ipInfo->getFullIpDetails("8.8.8.8");
} catch (RateLimitExceedException $e) {
    echo 'Caught exception: ',  $e->getMessage(), "\n";
}

错误IP异常

可能发生您的API调用尝试获取错误IP的信息的情况。您可以通过捕获WrongIpException来处理它。

use DavidePastore\Ipinfo\Exception\WrongIpException;

try {
    $host = $ipInfo->getFullIpDetails("qwerty");
} catch (WrongIpException $e) {
    echo 'Caught exception: ',  $e->getMessage(), "\n";
}

问题

如果您有问题,请在此处打开一个