File tree Expand file tree Collapse file tree 2 files changed +64
-0
lines changed Expand file tree Collapse file tree 2 files changed +64
-0
lines changed Original file line number Diff line number Diff line change 7272 'continent_path ' => storage_path ('app/continents.json ' ),
7373 'lang ' => 'en ' ,
7474 ],
75+
76+ 'ipgeolocation ' => [
77+ 'class ' => \Torann \GeoIP \Services \IPGeoLocation::class,
78+ 'secure ' => true ,
79+ 'key ' => env ('IPGEOLOCATION_KEY ' ),
80+ 'continent_path ' => storage_path ('app/continents.json ' ),
81+ 'lang ' => 'en ' ,
82+ ],
7583
7684 ],
7785
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+ class IPGeoLocation extends AbstractService
10+ {
11+ /**
12+ * Http client instance.
13+ *
14+ * @var HttpClient
15+ */
16+ protected $ client ;
17+
18+ /**
19+ * The "booting" method of the service.
20+ *
21+ * @return void
22+ */
23+ public function boot ()
24+ {
25+ $ base = [
26+ 'base_uri ' => 'https://api.ipgeolocation.io/ '
27+ ];
28+
29+ if ($ this ->config ('key ' )) {
30+ $ base ['base_uri ' ] = $ base ['base_uri ' ]."ipgeo?apiKey= " . $ this ->config ('key ' );
31+ }
32+
33+ $ this ->client = new HttpClient ($ base );
34+ }
35+
36+ /**
37+ * {@inheritdoc}
38+ */
39+
40+ public function locate ($ ip )
41+ {
42+ // Get data from client
43+ $ data = $ this ->client ->get ('&ip= ' . $ ip );
44+
45+ // Verify server response
46+ if ($ this ->client ->getErrors () !== null ) {
47+ throw new Exception ('Request failed ( ' . $ this ->client ->getErrors () . ') ' );
48+ }
49+
50+ // Parse body content
51+ $ json = json_decode ($ data [0 ],true );
52+
53+ return $ this ->hydrate ($ json );
54+
55+ }
56+ }
You can’t perform that action at this time.
0 commit comments