palauaandsons/cloudflare-geoip

基于Laravel的Cloudflare GeoIP助手

1.1.1 2024-06-03 17:50 UTC

This package is auto-updated.

Last update: 2024-09-03 18:24:33 UTC


README

根据Cloudflare头部信息获取访客的地理位置。

注意

确保在Cloudflare仪表板中启用了 访客位置 & IP地理位置

安装

composer require palauaandsons/cloudflare-geoip

配置

运行以下命令以发布配置。请确保修改配置文件,特别是当Cloudflare头部信息不存在时使用默认位置。

php artisan vendor:publish --tag=cloudflare-geoip-config

使用方法

<?php

namespace App\Http\Controllers;

use PalauaAndSons\CloudflareGeoIp\Facades\GeoIp;
use App\Models\Users;

class RegisterController extends Controller {
    public function __invoke(): User
    {
        $location = GeoIp::getLocation();

        return User::create([
            'register_ip' => $location->ip
        ]);
    }
}

位置对象

\PalauaAndSons\CloudflareGeoIp\Location {
    +ip: "192.182.88.29"
    +city: "New Haven"
    +country: "US"
    +continent: "NA"
    +latitude: 41.31
    +longitude: -72.92
    +postalCode: "06510"
    +region: "Connecticut"
    +regionCode: "CT"
    +timezone: "America/New_York"
    // pseudo fields
    +iso_code: 'US' // same as country
    +isoCode: 'US' // same as country
    +state: 'CT' // same as regionCode
    +state_name: 'Connecticut' // same as region
    +stateName: 'Connecticut' // same as region
    +postal_code: '06510' // same as postal_code
    +lat: '41.31' // same as latitude
    +lon: -72.92 // same as longitude

    +toArray(): array
}