|
| 1 | +# IP-Api Laravel - IP Address Information Lookup |
| 2 | + |
| 3 | +[](https://packagist.org/packages/harrisonratcliffe/ip-api-laravel) |
| 4 | +[](https://packagist.org/packages/harrisonratcliffe/ip-api-laravel) |
| 5 | +[](https://packagist.org/packages/harrisonratcliffe/ip-api-laravel) |
| 6 | +[](https://packagist.org/packages/harrisonratcliffe/ip-api-laravel) |
| 7 | + |
| 8 | +The `IpApiLaravel` package provides an easy-to-use interface for looking up information about IP addresses using the ip-api-laravel.com API. It allows you to fetch various details about an IP address, such as geographical location, time zone, currency, and more. |
| 9 | + |
| 10 | +This package is a fork of [el-factory/ip-api](https://github.com/el-factory/ip-api) with additional features. |
| 11 | + |
| 12 | +## Requirements |
| 13 | + |
| 14 | +- PHP 7.2 or higher |
| 15 | +- Laravel 7 or higher |
| 16 | + |
| 17 | +## Installation |
| 18 | + |
| 19 | +The `IpApiLaravel` package requires Laravel version 7 or higher. |
| 20 | + |
| 21 | +To install the package, you can use Composer: |
| 22 | + |
| 23 | +```bash |
| 24 | +composer require harrisonratcliffe/ip-api-laravel |
| 25 | +``` |
| 26 | + |
| 27 | +## Configuration |
| 28 | + |
| 29 | +After installing the package, you can optionally publish the configuration file to customize the default settings: |
| 30 | + |
| 31 | +```bash |
| 32 | +php artisan vendor:publish --tag=ip-api-laravel-config |
| 33 | +``` |
| 34 | + |
| 35 | +This will create a `ip-api-laravel.php` file in the `config` directory of your Laravel application. You can modify the default configuration values in this file according to your specific requirements. |
| 36 | + |
| 37 | +## Usage |
| 38 | + |
| 39 | +### IP Address Lookup |
| 40 | + |
| 41 | +To perform an IP address lookup, you can use the `lookup` method on the `IpApiLaravel` class: |
| 42 | + |
| 43 | +```php |
| 44 | +use Harrisonratcliffe\IpApiLaravel\IpApiLaravel; |
| 45 | + |
| 46 | +$ipDetails = IpApiLaravel::default('188.216.103.93')->lookup(); |
| 47 | +``` |
| 48 | + |
| 49 | +This will return an array with the details of the provided IP address. |
| 50 | + |
| 51 | +### Additional Configuration |
| 52 | + |
| 53 | +You can also customize the IP address lookup by using the following methods: |
| 54 | + |
| 55 | +#### `fields(array $fields): IpApiLaravel` |
| 56 | + |
| 57 | +Set the fields to be included in the API response. The `$fields` parameter should be an array of fields you want to retrieve. |
| 58 | + |
| 59 | +```php |
| 60 | +$ipDetails = IpApiLaravel::default('188.216.103.93')->fields(['city', 'country', 'timezone'])->lookup(); |
| 61 | +``` |
| 62 | + |
| 63 | +#### `usingKey(string $apiKey): IpApiLaravel` |
| 64 | + |
| 65 | +By default, the package uses the API key provided in the config file. If you wish to send a different API key for a specific request, you can use the `usingKey` method to set the API key for that request. |
| 66 | + |
| 67 | +```php |
| 68 | +$ipDetails = IpApiLaravel::default('188.216.103.93')->usingKey('YOUR_API_KEY')->lookup(); |
| 69 | +``` |
| 70 | + |
| 71 | +#### `retry(int $times, int $sleep): IpApiLaravel` |
| 72 | + |
| 73 | +Set the retry configuration for failed API requests. The `$times` parameter represents the number of retry attempts, and the `$sleep` parameter specifies the number of seconds to sleep between retries. |
| 74 | + |
| 75 | +```php |
| 76 | +$ipDetails = IpApiLaravel::default('188.216.103.93')->retry(3, 2)->lookup(); |
| 77 | +``` |
| 78 | + |
| 79 | +#### `lang(string $language): IpApiLaravel` |
| 80 | + |
| 81 | +Set the language for the API response. The `$language` parameter should be a two-letter language code. |
| 82 | + |
| 83 | +```php |
| 84 | +$ipDetails = IpApiLaravel::default('188.216.103.93')->lang('en')->lookup(); |
| 85 | +``` |
| 86 | + |
| 87 | +#### `withHeaders(): IpApiLaravel` |
| 88 | + |
| 89 | +Include request limit and remaining requests headers in the API response. |
| 90 | + |
| 91 | +```php |
| 92 | +$ipDetails = IpApiLaravel::default('188.216.103.93')->withHeaders()->lookup(); |
| 93 | +``` |
| 94 | + |
| 95 | +#### `timeout(int $seconds): IpApiLaravel` |
| 96 | + |
| 97 | +Set a timeout for the API request. The `$seconds` parameter specifies the maximum number of seconds to wait for a response. If the request times out, an exception will be thrown. |
| 98 | + |
| 99 | +```php |
| 100 | +$ipDetails = IpApiLaravel::default('188.216.103.93')->timeout(10)->lookup(); // Set timeout to 10 seconds |
| 101 | +``` |
| 102 | + |
| 103 | +### Console Command - `ip-api-laravel:connection` |
| 104 | + |
| 105 | +The `IpApiLaravel` package also includes a console command named `ip-api-laravel:connection` that allows you to test the connection to the ip-api-laravel.com API with a specific IP address or your public IP address (if not provided). |
| 106 | + |
| 107 | +To use the command, run the following Artisan command: |
| 108 | + |
| 109 | +```bash |
| 110 | +php artisan ip-api-laravel:connection {ip?} |
| 111 | +``` |
| 112 | + |
| 113 | +- `{ip}` (optional): The IP address to test with. If not provided, the command will use your public IP address. |
| 114 | + |
| 115 | +The command will display the details retrieved from the ip-api-laravel.com API for the provided IP address. |
| 116 | + |
| 117 | +### Example |
| 118 | + |
| 119 | +Here's an example of using the `ip-api-laravel:connection` command: |
| 120 | + |
| 121 | +```bash |
| 122 | +php artisan ip-api-laravel:connection 188.216.103.93 |
| 123 | +``` |
| 124 | + |
| 125 | +This will test the connection to ip-api-laravel.com API with the specified IP address and display the details retrieved. |
| 126 | + |
| 127 | +If no IP address is provided, the command will automatically use your public IP address: |
| 128 | + |
| 129 | +```bash |
| 130 | +php artisan ip-api-laravel:connection |
| 131 | +``` |
| 132 | + |
| 133 | +## Exceptions |
| 134 | + |
| 135 | +The `IpApiLaravel` class may throw the following exceptions: |
| 136 | + |
| 137 | +- `Exception`: If the provided IP address is invalid or reserved. |
| 138 | +- `RequestException`: If an error occurs while making the API request. |
| 139 | + |
| 140 | +## License |
| 141 | + |
| 142 | +The `IpApiLaravel` package is open-source software licensed under the [MIT License](https://opensource.org/licenses/MIT). Feel free to use and modify it according to your needs. |
0 commit comments