victorybiz / geoip-location
根据访客的IP地址获取网站访客的地理位置。支持Laravel和PHP(非Laravel)项目。
v1.1.10
2021-12-29 10:36 UTC
Requires
- php: >=7.2
- guzzlehttp/guzzle: ^6.0 || ^7.0
- ipinfo/ipinfo: ^2.2
Requires (Dev)
- phpunit/phpunit: ^8.0
README
根据访客的IP地址获取网站访客的地理位置。支持Laravel和PHP(非Laravel)项目。
安装
使用composer安装,从命令行运行
$ composer require victorybiz/geoip-location
Laravel项目
或者,您可以将 "victorybiz/geoip-location": "^1.1"
添加到 composer.json 文件的 require
部分,然后您需要运行 composer install
或 composer update
来下载它并更新自动加载器。
如果您使用 Laravel >= 5.5,则可以跳过此步骤,转到
使用方法
如果您使用 Laravel < 5.5,则需要将服务提供程序注册到应用程序中。打开
config/app.php
并定位到providers
键。
'providers' => [ Victorybiz\GeoIPLocation\GeoIPLocationServiceProvider::class, ]
然后在 config/app.php 中添加 GeoIPLocation 别名
'aliases' => [ 'GeoIPLocation' => Victorybiz\GeoIPLocation\Facades\GeoIPLocationFacade::class, ]
Laravel项目中的使用
请使用 GeoIPLocation
Facade
use GeoIPLocation; echo GeoIPLocation::getIP(); // Return client IP
PHP(非Laravel)项目
在您的php脚本中引入vendor自动加载文件。
require_once 'path/to/vendor/autoload.php';
使用方法
本地主机IP 127.0.0.1
,192.168.65.0
,172.18.0.1
和 ::1
将返回 169.159.82.111
以断言有效的地理响应。
use Victorybiz\GeoIPLocation\GeoIPLocation; $geoip = new GeoIPLocation(); // OR $geoip = new GeoIPLocation([ 'ip' => null, // Set IP. Default is NULL, will be auto set by the package 'baseCurrency' => 'USD', // Set base currency for exchange rate. Default is USD ]);
或者
$geoip = new \Victorybiz\GeoIPLocation\GeoIPLocation(); // OR $geoip = new \Victorybiz\GeoIPLocation\GeoIPLocation([ 'ip' => null, // Set IP. Default is NULL, will be auto set by the package 'baseCurrency' => 'USD', // Set base currency for exchange rate. Default is USD ]);
您可以开始使用这个包了,探索一下
echo $geoip->getIP(); // Return client IP echo $geoip->setIP('0.0.0.0'); // Set an IP to get its geographical location echo $geoip->getCity(); // Return client IP City (null if none) echo $geoip->getRegion(); // Return client IP Region (null if none) echo $geoip->getRegionCode(); // Return client IP Region Code (null if none) echo $geoip->getCountry(); // Return client IP Country echo $geoip->getCountryCode(); // Return client IP Country Code echo $geoip->getContinent(); // Return client IP Continent echo $geoip->getContinentCode(); // Return client IP Continent Code echo $geoip->getPostalCode(); // Return client IP Postal Code (null if none) echo $geoip->getLatitude(); // Return client IP Latitude (null if none) echo $geoip->getLongitude(); // Return client IP Longitude (null if none) echo $geoip->getCurrencyCode(); // Return client IP Country Currency Code (null if none) echo $geoip->getCurrencySymbol(); // Return client IP Country Currency Symbol (null if none) echo $geoip->getCurrencyExchangeRate(); // Return client IP Country Currency Exchange Rate against NGN (null if none) echo $geoip->getLocation(); // Return client IP Location string (city, region, country)
网络服务
- geoPlugin - GeoIP Location 是对来自 geoPlugin 的地理位置网络服务的封装。不要犹豫,查看 Premium geoPlugin 访问。
错误报告和问题跟踪
请使用问题跟踪器来报告错误、提出功能请求、额外的网络服务请求和安全问题。