nawrasbukhari/geo-content

GeoContent 是一个 Laravel 扩展包,它提供了一种简单的方法来根据用户的地理位置管理内容。

0.2.0 2023-08-11 09:24 UTC

This package is auto-updated.

Last update: 2024-09-11 11:59:04 UTC


README

Laravel Geo-Content 扩展包提供了一个方便的方法,使用 FreeIPAPI 服务获取地理位置信息。此扩展包旨在获取 IP 地址的地理位置数据,包括国家名称、国家代码、大洲等详细信息。

安装

composer require nawrasbukhari/geo-content

您可以使用以下命令发布配置文件:

php artisan vendor:publish --tag="geo-content-config"

这是已发布配置文件的内容

return [
    'freeipapi_base_url' => env('FREEIPAPI_BASE_URL', 'https://freeipapi.com/api/json/'),
    'freeipapi_key' => env('FREEIPAPI_KEY'),
    'freeipapi_ssl' => env('FREEIPAPI_SSL', false),
    'timeout' => env('FREEIPAPI_TIMEOUT', 30),
    'testing_ip_address' => env('FREEIPAPI_TESTING_IP_ADDRESS', '208.67.222.222'),
    'usual_localhost_ip' => [
        '127.0.0.1',
        '::1',
        'localhost',
    ],
];

使用方法

仅向来自美国的用户显示内容

use NawrasBukhari\GeoContent\Facades\GeoContent;

$geoContent = new GeoContent();

if ($geoContent->country('United States of America')) {
    // Display restricted content here
}

允许特定大陆的内容同样,您可以使用 continent 方法仅向特定大陆的用户显示内容

use NawrasBukhari\GeoContent\Facades\GeoContent;

$geoContent = new GeoContent();

if ($geoContent->continent('AM')) {
    // Display restricted content here
}

禁止特定国家的内容您可以使用 onlyShowInCountry()onlyShowInCountryCode() 方法向特定国家的用户显示内容

use NawrasBukhari\GeoContent\Facades\GeoContent;

// Create an instance of the GeoContent class
$geoContent = new GeoContent();

// Hide content from users in Russia
if ($geoContent->onlyShowInCountry('Russia')) {
    // Display non-restricted content here for users from Russia
}

测试

composer test

变更日志

有关最近更改的更多信息,请参阅 CHANGELOG

致谢

许可证

MIT 许可证(MIT)。有关更多信息,请参阅 许可证文件