定制 jord-jd/php-geolocation 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

jord-jd/php-geolocation

最新稳定版本:v5.0.0

Composer 安装命令:

composer require jord-jd/php-geolocation

包简介

PHP library that determines the country of an IP address

README 文档

README

CI

This package provides a PHP library that determines the country of an IP address.

Installation

You can easily install PHP Geolocation with composer.

composer require jord-jd/php-geolocation

Usage

The most simple usage of PHP Geolocation is to create a new Locator object and call its getCountryByIP method.

// Get country of the current request's IP address
$country = (new Locator)->getCountryByIP($_SERVER['REMOTE_ADDR']);

// Get country of a specific IP address
$country = (new Locator)->getCountryByIP('93.184.216.34');

// Returns a Country object
/*
object(JordJD\Countries\Country)#4693 (16) {
  ["name"]=>
  string(13) "United States"
  ["officialName"]=>
  string(24) "United States of America"
  // etc...
}
*/

Caching

You can configure PHP Geolocation to use any PSR-6 compliant caching library. This is easily done using the setCache method.

The following example configures a file cache (provided by the cache/filesystem-adapter package).

use League\Flysystem\Adapter\Local;
use League\Flysystem\Filesystem;
use Cache\Adapter\Filesystem\FilesystemCachePool;

$filesystemAdapter = new Local(__DIR__.'/');
$filesystem = new Filesystem($filesystemAdapter);
$cachePool = new FilesystemCachePool($filesystem);

$locator = new Locator;
$locator->setCache($cachePool);

$country = $locator->getCountryByIP('93.184.216.34');

Alternative location providers

By default, PHP Geolocation will try to use the operating system's native whois command to determine the IP address. If you wish you can use an alternative location provider. This can be done using the setLocationProvider method, as follows.

$locator = new Locator;
$locator->setLocationProvider(new IpStack('my_ip_stack_api_key');

$country = $locator->getCountryByIP('93.184.216.34');

To get a free api key sign up at Ip Stack's website.

If you wish to develop your own location provider, simply create a new class that implements the LocationProviderInterface provided in this package. See the existing WhoIs and FreeGeoIP location provider classes if you need help creating your own location provider.

统计信息

  • 总下载量: 4
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 7
  • 点击次数: 5
  • 依赖项目数: 2
  • 推荐数: 0

GitHub 信息

  • Stars: 7
  • Watchers: 2
  • Forks: 4
  • 开发语言: PHP

其他信息

  • 授权协议: LGPL-3.0-only
  • 更新时间: 2026-02-14

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固