Skip to content

Commit e113bea

Browse files
committed
Added IP2Proxy web service
1 parent 34c3318 commit e113bea

File tree

4 files changed

+104
-40
lines changed

4 files changed

+104
-40
lines changed

README.md

Lines changed: 76 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,65 +4,115 @@
44

55
IP2Proxy Laravel extension enables the user to query an IP address if it was being used as open proxy, web proxy, VPN anonymizer and TOR exits.
66

7+
*Note: This extension works in Laravel 6, Laravel 7 and Laravel 8.*
8+
79

810
## INSTALLATION
911

1012
1. Run the command: `composer require ip2location/ip2proxy-laravel` to download the package into the Laravel platform.
1113
2. Edit `config/app.php` and add the below line in 'providers' section:
1214
`Ip2location\IP2ProxyLaravel\IP2ProxyLaravelServiceProvider::class,`
1315
3. Then publish the config file by:
14-
`php artisan vendor:publish --provider=Ip2location\IP2ProxyLaravel\IP2ProxyLaravelServiceProvider --force`
15-
4. Download IP2Proxy BIN database
16+
`php artisan vendor:publish --provider='Ip2location\IP2ProxyLaravel\IP2ProxyLaravelServiceProvider' --force`
17+
18+
## USAGE
19+
20+
IP2Proxy Laravel extension is able to query the IP address proxy information from either BIN database or web service. This section will explain how to use this extension to query from BIN database and web service.
21+
22+
### BIN DATABASE
23+
24+
1. Download IP2Proxy BIN database
1625
- IP2Proxy free LITE database at https://lite.ip2location.com
1726
- IP2Proxy commercial database at https://www.ip2location.com/proxy-database
18-
5. Create a folder named as `ip2proxy` in the `database` directory.
19-
6. Unzip and copy the BIN file into `database/ip2proxy/` folder.
20-
7. Rename the BIN file to IP2PROXY.BIN.
27+
2. To use IP2Proxy databases, create a folder named as `ip2proxy` in the `database` directory.
28+
3. Unzip and copy the BIN file into `database/ip2proxy/` folder.
29+
4. Rename the BIN file to IP2PROXY.BIN.
30+
5. Create a **TestController** in Laravel using the below command line
31+
```
32+
php artisan make:controller TestController
33+
```
34+
6. Open the **app/Http/Controllers/TestController.php** in any text editor.
35+
7. To use IP2Proxy databases, add the below lines into the controller file
36+
```php
37+
<?php
2138

39+
namespace App\Http\Controllers;
2240

23-
## USAGE
41+
use Illuminate\Http\Request;
42+
use IP2ProxyLaravel; //use IP2ProxyLaravel class
2443

25-
In this tutorial, we will show you on how to create a **TestController** to display the IP information.
44+
class TestController extends Controller
45+
{
46+
//Create a lookup function for display
47+
public function lookup(){
48+
//Try query the geolocation information of 1.2.3.4 IP address
49+
$records = IP2ProxyLaravel::get('1.2.3.4', 'bin');
50+
51+
echo '<p><strong>IP Address: </strong>' . $records['ipAddress'] . '</p>';
52+
echo '<p><strong>IP Number: </strong>' . $records['ipNumber'] . '</p>';
53+
echo '<p><strong>IP Version: </strong>' . $records['ipVersion'] . '</p>';
54+
echo '<p><strong>Country Code: </strong>' . $records['countryCode'] . '</p>';
55+
echo '<p><strong>Country: </strong>' . $records['countryName'] . '</p>';
56+
echo '<p><strong>State: </strong>' . $records['regionName'] . '</p>';
57+
echo '<p><strong>City: </strong>' . $records['cityName'] . '</p>';
58+
echo '<p><strong>Proxy Type: </strong>' . $records['proxyType'] . '</p>';
59+
echo '<p><strong>Is Proxy: </strong>' . $records['isProxy'] . '</p>';
60+
echo '<p><strong>ISP: </strong>' . $records['isp'] . '</p>';
61+
}
62+
}
63+
```
64+
8. Add the following line into the *routes/web.php* file.
65+
```
66+
Route::get('test', 'TestController@lookup');
67+
```
68+
9. Enter the URL <your domain>/public/test and run. You should see the information of **1.2.3.4** IP address.
2669

27-
1. Create a **TestController** in Laravel using the below command line
70+
### WEB SERVICE
71+
72+
1. To use IP2Proxy Web Service, create a new file called "site_vars.php" in `config` directory.
73+
2. In the site_vars.php, save the following contents:
74+
```php
75+
<?php
76+
return [
77+
'IP2ProxyAPIKey' => 'your_api_key', // Required. Your IP2Proxy API key.
78+
'IP2ProxyPackage' => 'PX1', // Required. Choose the package you would like to use.
79+
'IP2ProxyUsessl' => false, // Optional. Use https or http.
80+
];
2881
```
29-
php artisan make:controller TestController
82+
3. Create a **TestController** in Laravel using the below command line
3083
```
31-
2. Open the **app/Http/Controllers/TestController.php** in any text editor.
32-
3. Add the below lines into the controller file.
84+
php artisan make:controller TestController
3385
```
86+
4. Open the **app/Http/Controllers/TestController.php** in any text editor.
87+
5. To use IP2Proxy Web Service, add the below lines into the controller file.
88+
```php
3489
<?php
3590

3691
namespace App\Http\Controllers;
3792

3893
use Illuminate\Http\Request;
94+
3995
use IP2ProxyLaravel; //use IP2ProxyLaravel class
4096

4197
class TestController extends Controller
4298
{
4399
//Create a lookup function for display
44-
public function lookup(){
100+
public function lookup(){
45101
//Try query the geolocation information of 1.2.3.4 IP address
46-
$record = IP2ProxyLaravel::get('1.2.3.4');
47-
48-
echo '<p><strong>IP Address: </strong>' . $record['ipAddress'] . '</p>';
49-
echo '<p><strong>IP Number: </strong>' . $record['ipNumber'] . '</p>';
50-
echo '<p><strong>IP Version: </strong>' . $record['ipVersion'] . '</p>';
51-
echo '<p><strong>Country Code: </strong>' . $record['countryCode'] . '</p>';
52-
echo '<p><strong>Country: </strong>' . $record['countryName'] . '</p>';
53-
echo '<p><strong>State: </strong>' . $record['regionName'] . '</p>';
54-
echo '<p><strong>City: </strong>' . $record['cityName'] . '</p>';
55-
echo '<p><strong>Proxy Type: </strong>' . $record['proxyType'] . '</p>';
56-
echo '<p><strong>Is Proxy: </strong>' . $record['isProxy'] . '</p>';
57-
echo '<p><strong>ISP: </strong>' . $record['isp'] . '</p>';
102+
$records = IP2ProxyLaravel::get('1.2.3.4', 'ws');
103+
104+
echo '<pre>';
105+
print_r($records);
106+
echo '</pre>';
58107
}
59108
}
109+
60110
```
61-
4. Add the following line into the *routes/web.php* file.
111+
6. Add the following line into the *routes/web.php* file.
62112
```
63113
Route::get('test', 'TestController@lookup');
64114
```
65-
5. Enter the URL <your domain>/public/test and run. You should see the information of **1.2.3.4** IP address.
115+
7. Enter the URL <your domain>/public/test and run. You should see the information of **1.2.3.4*** IP address.
66116

67117
## DEPENDENCIES (IP2PROXY BIN DATA FILE)
68118

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"minimum-stability": "stable",
1818
"require": {
1919
"php": ">=5.4",
20-
"ip2location/ip2proxy-php": "1.*"
20+
"ip2location/ip2proxy-php": ">=4"
2121
},
2222
"autoload": {
2323
"psr-4": {

src/IP2ProxyLaravel.php

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,36 @@
44

55
class IP2ProxyLaravel
66
{
7-
8-
public function get($ip)
7+
private function load($mode)
98
{
10-
$db = new \IP2Proxy\Database();
11-
$db->open($this->getDatabasePath(), \IP2Proxy\Database::FILE_IO);
12-
13-
$records = $db->getAll($ip);
14-
15-
$db->close();
16-
17-
return $records;
9+
if ($mode == 'bin')
10+
{
11+
$this->$db = new \IP2Proxy\Database($this->getDatabasePath(), \IP2PROXY\Database::FILE_IO);
12+
} else if ($mode == 'ws')
13+
{
14+
$apikey = \Config::get('site_vars.IP2ProxyAPIKey');
15+
$package = (null !== \Config::get('site_vars.IP2ProxyPackage')) ? \Config::get('site_vars.IP2ProxyPackage') : 'PX1';
16+
$ssl = (null !== \Config::get('site_vars.IP2ProxyUsessl')) ? \Config::get('site_vars.IP2ProxyUsessl') : false;
17+
$this->ws = new \IP2Proxy\WebService($apikey, $package, $ssl);
18+
}
1819
}
1920

2021
private function getDatabasePath()
2122
{
2223
return config('ip2proxylaravel.ip2proxy.local.path');
2324
}
2425

26+
public function get($ip, $mode = 'bin')
27+
{
28+
$this->load($mode);
29+
if ($mode == 'bin')
30+
{
31+
$records = $this->db->lookup($ip, \IP2PROXY\Database::ALL);
32+
} else if ($mode == 'ws')
33+
{
34+
$records = $this->ws->lookup($ip);
35+
}
36+
return $records;
37+
}
38+
2539
}

src/IP2ProxyLaravelServiceProvider.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ public function boot()
2424
*/
2525
public function register()
2626
{
27-
//Dynamically add IP2ProxyLaravel alias
28-
AliasLoader::getInstance()->alias('IP2ProxyLaravel', 'Ip2location\IP2ProxyLaravel\Facade\IP2ProxyLaravel');
29-
27+
//Dynamically add IP2ProxyLaravel alias
28+
AliasLoader::getInstance()->alias('IP2ProxyLaravel', 'Ip2location\IP2ProxyLaravel\Facade\IP2ProxyLaravel');
29+
3030
$config = __DIR__.'/Config/ip2proxylaravel.php';
3131

3232
$this->publishes([

0 commit comments

Comments
 (0)