File tree Expand file tree Collapse file tree 2 files changed +58
-0
lines changed Expand file tree Collapse file tree 2 files changed +58
-0
lines changed Original file line number Diff line number Diff line change 8181 'lang ' => 'en ' ,
8282 ],
8383
84+ 'ipdata ' => [
85+ 'class ' => \Torann \GeoIP \Services \IPData::class,
86+ 'key ' => env ('IPDATA_API_KEY ' ),
87+ 'secure ' => true ,
88+ ],
89+
8490 ],
8591
8692 /*
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace Torann \GeoIP \Services ;
4+
5+ use Exception ;
6+ use Torann \GeoIP \Support \HttpClient ;
7+
8+ /**
9+ * Class GeoIP
10+ * @package Torann\GeoIP\Services
11+ */
12+ class IPData extends AbstractService
13+ {
14+ /**
15+ * Http client instance.
16+ *
17+ * @var HttpClient
18+ */
19+ protected $ client ;
20+
21+ /**
22+ * The "booting" method of the service.
23+ *
24+ * @return void
25+ */
26+ public function boot ()
27+ {
28+ $ this ->client = new HttpClient ([
29+ 'base_uri ' => 'https://api.ipdata.co/ ' ,
30+ 'query ' => [
31+ 'api-key ' => $ this ->config ('key ' ),
32+ ],
33+ ]);
34+ }
35+
36+ /**
37+ * {@inheritdoc}
38+ * @throws Exception
39+ */
40+ public function locate ($ ip )
41+ {
42+ // Get data from client
43+ $ data = $ this ->client ->get ($ ip );
44+
45+ // Verify server response
46+ if ($ this ->client ->getErrors () !== null || empty ($ data [0 ])) {
47+ throw new Exception ('Request failed ( ' . $ this ->client ->getErrors () . ') ' );
48+ }
49+
50+ return $ this ->hydrate (json_decode ($ data [0 ], true ));
51+ }
52+ }
You can’t perform that action at this time.
0 commit comments