4
4
5
5
class IP2ProxyLaravel
6
6
{
7
+ private $ db ;
8
+ private $ ws ;
9
+
7
10
private function load ($ mode )
8
11
{
9
12
if ($ mode == 'bin ' )
10
13
{
11
14
$ this ->db = new \IP2Proxy \Database ($ this ->getDatabasePath (), \IP2PROXY \Database::FILE_IO );
12
15
} else if ($ mode == 'ws ' )
13
16
{
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 );
17
+ if (!config ()->has ('site_vars.IP2LocationioAPIKey ' ))
18
+ {
19
+ $ apikey = \Config::get ('site_vars.IP2ProxyAPIKey ' );
20
+ $ package = (null !== \Config::get ('site_vars.IP2ProxyPackage ' )) ? \Config::get ('site_vars.IP2ProxyPackage ' ) : 'PX1 ' ;
21
+ $ ssl = (null !== \Config::get ('site_vars.IP2ProxyUsessl ' )) ? \Config::get ('site_vars.IP2ProxyUsessl ' ) : false ;
22
+ $ this ->ws = new \IP2Proxy \WebService ($ apikey , $ package , $ ssl );
23
+ }
18
24
}
19
25
}
20
26
@@ -31,7 +37,49 @@ public function get($ip, $mode = 'bin')
31
37
$ records = $ this ->db ->lookup ($ ip , \IP2PROXY \Database::ALL );
32
38
} else if ($ mode == 'ws ' )
33
39
{
34
- $ records = $ this ->ws ->lookup ($ ip );
40
+ if (config ()->has ('site_vars.IP2LocationioAPIKey ' ))
41
+ {
42
+ // Use IP2Location.io API
43
+ $ ioapi_baseurl = 'https://api.ip2location.io/? ' ;
44
+ $ params = [
45
+ 'key ' => \Config::get ('site_vars.IP2LocationioAPIKey ' ),
46
+ 'ip ' => $ ip ,
47
+ 'lang ' => ((config ()->has ('site_vars.IP2LocationioLanguage ' )) ? \Config::get ('site_vars.IP2LocationioLanguage ' ) : '' ),
48
+ ];
49
+ // Remove parameters without values
50
+ $ params = array_filter ($ params );
51
+ $ url = $ ioapi_baseurl . http_build_query ($ params );
52
+ $ ch = curl_init ();
53
+ curl_setopt ($ ch , CURLOPT_URL , $ url );
54
+ curl_setopt ($ ch , CURLOPT_FAILONERROR , 0 );
55
+ curl_setopt ($ ch , CURLOPT_FOLLOWLOCATION , 1 );
56
+ curl_setopt ($ ch , CURLOPT_RETURNTRANSFER , 1 );
57
+ curl_setopt ($ ch , CURLOPT_SSL_VERIFYPEER , 0 );
58
+ curl_setopt ($ ch , CURLOPT_TIMEOUT , 30 );
59
+
60
+ $ response = curl_exec ($ ch );
61
+
62
+ if (!curl_errno ($ ch ))
63
+ {
64
+ if (($ data = json_decode ($ response , true )) === null )
65
+ {
66
+ return false ;
67
+ }
68
+ if (array_key_exists ('error ' , $ data ))
69
+ {
70
+ throw new \Exception (__CLASS__ . ': ' . $ data ['error ' ]['error_message ' ], $ data ['error ' ]['error_code ' ]);
71
+ }
72
+ return $ data ;
73
+ }
74
+
75
+ curl_close ($ ch );
76
+
77
+ return false ;
78
+
79
+ } else
80
+ {
81
+ $ records = $ this ->ws ->lookup ($ ip );
82
+ }
35
83
}
36
84
return $ records ;
37
85
}
0 commit comments