File tree 2 files changed +65
-0
lines changed
2 files changed +65
-0
lines changed Original file line number Diff line number Diff line change 87
87
'secure ' => true ,
88
88
],
89
89
90
+ 'ipfinder ' => [
91
+ 'class ' => \Torann \GeoIP \Services \IPFinder::class,
92
+ 'key ' => env ('IPFINDER_API_KEY ' ),
93
+ 'secure ' => true ,
94
+ 'locales ' => ['en ' ],
95
+ ],
96
+
90
97
],
91
98
92
99
/*
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace Torann \GeoIP \Services ;
4
+
5
+ use Exception ;
6
+ use Illuminate \Support \Arr ;
7
+ use Torann \GeoIP \Support \HttpClient ;
8
+
9
+ /**
10
+ * Class GeoIP
11
+ * @package Torann\GeoIP\Services
12
+ */
13
+ class IPFinder extends AbstractService
14
+ {
15
+ /**
16
+ * Http client instance.
17
+ *
18
+ * @var HttpClient
19
+ */
20
+ protected $ client ;
21
+
22
+ /**
23
+ * The "booting" method of the service.
24
+ *
25
+ * @return void
26
+ */
27
+ public function boot ()
28
+ {
29
+ $ this ->client = new HttpClient ([
30
+ 'base_uri ' => 'https://api.ipfinder.io/v1/ ' ,
31
+ 'headers ' => [
32
+ 'User-Agent ' => 'Laravel-GeoIP-Torann ' ,
33
+ ],
34
+ 'query ' => [
35
+ 'token ' => $ this ->config ('key ' ),
36
+ ],
37
+ ]);
38
+ }
39
+
40
+ /**
41
+ * {@inheritdoc}
42
+ * @throws Exception
43
+ */
44
+ public function locate ($ ip )
45
+ {
46
+ // Get data from client
47
+ $ data = $ this ->client ->get ($ ip );
48
+
49
+ // Verify server response
50
+ if ($ this ->client ->getErrors () !== null || empty ($ data [0 ])) {
51
+ throw new Exception ('Request failed ( ' . $ this ->client ->getErrors () . ') ' );
52
+ }
53
+
54
+ $ json = json_decode ($ data [0 ], true );
55
+
56
+ return $ this ->hydrate ($ json );
57
+ }
58
+ }
You can’t perform that action at this time.
0 commit comments