ok/ipstack-client

Ipstack API 的 PHP 封装

2.1 2021-11-17 14:10 UTC

This package is auto-updated.

Last update: 2024-09-17 20:07:37 UTC


README

Latest Stable Version Total Downloads License

ipstack-client

Ipstack API 的 PHP 封装

使用 composer 安装

composer require ok/ipstack-client

基本用法

Location 对象获取数据

$client = new OK\Ipstack\Client('api_key');
$location = $client->get('134.201.250.155', false);
var_dump($location);

结果

class OK\Ipstack\Entity\Location#3 (12) {
  protected $city =>
  string(11) "Los Angeles"
  protected $continentCode =>
  string(2) "NA"
  protected $continentName =>
  string(13) "North America"
  protected $countryCode =>
  string(2) "US"
  protected $countryName =>
  string(13) "United States"
  protected $regionCode =>
  string(2) "CA"
  protected $regionName =>
  string(10) "California"
  protected $zip =>
  string(5) "90026"
  protected $latitude =>
  double(34.0766)
  protected $longitude =>
  double(-118.2646)
  protected $ip =>
  string(15) "134.201.250.155"
  protected $valid =>
  bool(true)
}

以简单数组获取数据

$client = new OK\Ipstack\Client('api_key');
$location = $client->get('134.201.250.155');
var_dump($location);

结果

array(13) {
  'ip' =>
  string(15) "134.201.250.155"
  'type' =>
  string(4) "ipv4"
  'continent_code' =>
  string(2) "NA"
  'continent_name' =>
  string(13) "North America"
  'country_code' =>
  string(2) "US"
  'country_name' =>
  string(13) "United States"
  'region_code' =>
  string(2) "CA"
  'region_name' =>
  string(10) "California"
  'city' =>
  string(11) "Los Angeles"
  'zip' =>
  string(5) "90026"
  'latitude' =>
  string(7) "34.0766"
  'longitude' =>
  string(9) "-118.2646"
  'location' =>
  array(8) {
    'geoname_id' =>
    string(7) "5368361"
    'capital' =>
    string(15) "Washington D.C."
    'languages' =>
    array(3) {
      'code' =>
      string(2) "en"
      'name' =>
      string(7) "English"
      'native' =>
      string(7) "English"
    }
    'country_flag' =>
    string(38) "http://assets.ipstack.com/flags/us.svg"
    'country_flag_emoji' =>
    string(8) "🇺🇸"
    'country_flag_emoji_unicode' =>
    string(15) "U+1F1FA U+1F1F8"
    'calling_code' =>
    string(1) "1"
    'is_eu' =>
    array(0) {
    }
  }
}

信息

可修改的输出参数

获取自定义结果的 参数

例如

$client = new OK\Ipstack\Client('api_key');
$client->getParams()->addField("calling_code");

数据提供者

现在,这里有可用的几个提供者

您可以通过在构造函数中传递上述类型之一作为第二个参数来更改提供者

例如

$client = new OK\Ipstack\Client('api_key', OK\Ipstack\Provider\ProviderFactory::TYPE_IPAPI);