davidepastore / ipinfo
ipinfo.io服务的包装器
v0.7.0
2022-12-20 19:17 UTC
Requires
- php: >=7.0
- ext-curl: *
- ext-json: *
Requires (Dev)
- phpunit/phpunit: 6.5.*
- scrutinizer/ocular: ~1.1
README
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
只读取地理数据(更快)
通过使用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"; }
问题
如果您有问题,只需在这里创建一个问题。