Skip to content

Commit 648115a

Browse files
initial commit
1 parent 1b59b39 commit 648115a

File tree

12 files changed

+453
-2
lines changed

12 files changed

+453
-2
lines changed

.gitattributes

Lines changed: 0 additions & 2 deletions
This file was deleted.

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,6 @@ Homestead.yaml
2121
Homestead.json
2222
/.vagrant
2323
.phpunit.result.cache
24+
25+
# Jet Brains
26+
/.idea

README.md

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
# IP-Api Laravel - IP Address Information Lookup
2+
3+
[![Latest Version on Packagist](https://img.shields.io/packagist/v/harrisonratcliffe/ip-api-laravel.svg?style=flat-square)](https://packagist.org/packages/harrisonratcliffe/ip-api-laravel)
4+
[![Total Downloads](https://img.shields.io/packagist/dt/harrisonratcliffe/ip-api-laravel?style=flat-square)](https://packagist.org/packages/harrisonratcliffe/ip-api-laravel)
5+
[![Repo Size](https://img.shields.io/github/repo-size/harrisonratcliffe/ip-api-laravel?style=flat-square)](https://packagist.org/packages/harrisonratcliffe/ip-api-laravel)
6+
[![Repo Size](https://img.shields.io/packagist/l/harrisonratcliffe/ip-api-laravel?style=flat-square)](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.

composer.json

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"name": "harrisonratcliffe/ip-api",
3+
"description": "A simple Laravel package to get IP address information from ip-api.com",
4+
"type": "library",
5+
"keywords": ["ip-api.com", "ip api laravel", "ip api php", "ip api"],
6+
"license": "MIT",
7+
"require": {
8+
"php": ">=7.2",
9+
"illuminate/support": ">=7.0"
10+
},
11+
"require-dev": {
12+
"pestphp/pest": "^2.9",
13+
"orchestra/testbench": "^8.5"
14+
},
15+
"autoload": {
16+
"psr-4": {
17+
"Harrisonratcliffe\\IpApiLaravel\\": "src/"
18+
}
19+
},
20+
"autoload-dev": {
21+
"psr-4": {
22+
"Harrisonratcliffe\\IpApiLaravel\\Tests\\": "tests/"
23+
}
24+
},
25+
"extra": {
26+
"laravel": {
27+
"providers": [
28+
"Harrisonratcliffe\\IpApiLaravel\\IpApiLaravelServiceProvider"
29+
]
30+
}
31+
},
32+
"config": {
33+
"allow-plugins": {
34+
"pestphp/pest-plugin": true
35+
}
36+
}
37+
}

config/ip-api.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
return [
4+
'key' => env('IP_API_KEY', ''),
5+
'url' => env('IP_API_URL', 'http://ip-api.com/json/'),
6+
'lang' => env('IP_API_LANG', 'en'),
7+
];

phpunit.xml.dist

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="vendor/autoload.php" backupGlobals="false"
3+
colors="true" processIsolation="false" stopOnFailure="false"
4+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.2/phpunit.xsd" cacheDirectory=".phpunit.cache"
5+
backupStaticProperties="false">
6+
<coverage/>
7+
<testsuites>
8+
<testsuite name="Unit">
9+
<directory suffix="Test.php">./tests</directory>
10+
</testsuite>
11+
</testsuites>
12+
<source>
13+
<include>
14+
<directory suffix=".php">src/</directory>
15+
</include>
16+
</source>
17+
<php>
18+
<env name="IP_API_KEY" value=""/>
19+
<env name="IP_API_URL" value="http://ip-api.com/json/"/>
20+
<env name="IP_API_LANG" value="en"/>
21+
</php>
22+
</phpunit>

src/Console/TestConnection.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace Harrisonratcliffe\IpApiLaravel\Console;
4+
5+
use Harrisonratcliffe\IpApiLaravel\IpApiLaravel;
6+
use Exception;
7+
use Illuminate\Console\Command;
8+
use Illuminate\Http\Client\RequestException;
9+
use Illuminate\Support\Facades\Http;
10+
11+
class TestConnection extends Command
12+
{
13+
protected $signature = 'ip-api:connection
14+
{ip? : The IP address to test with}';
15+
16+
protected $description = 'Test connection to ip-api.com';
17+
18+
/**
19+
* @throws RequestException
20+
* @throws Exception
21+
*/
22+
public function handle(): int
23+
{
24+
$ip = $this->argument('ip') ?? $this->publicIp();
25+
$response = IpApiLaravel::default($ip)->lookup();
26+
27+
$this->warn('Testing connection to ip-api.com api with ip: '.$ip);
28+
$this->info(json_encode($response));
29+
30+
return 0;
31+
}
32+
33+
/**
34+
* Get the public IP address
35+
*/
36+
private function publicIp(): string
37+
{
38+
$response = Http::get('https://httpbin.org/ip');
39+
$data = $response->json();
40+
41+
return $data['origin'];
42+
}
43+
}

0 commit comments

Comments
 (0)