1 | <?php |
||
10 | class Ipinfo |
||
11 | { |
||
12 | |||
13 | /** |
||
14 | * The base url of the ipinfo service. |
||
15 | * @var string |
||
16 | */ |
||
17 | const BASE_URL = "http://ipinfo.io/"; |
||
18 | |||
19 | /** |
||
20 | * The ip string. |
||
21 | * @var string |
||
22 | */ |
||
23 | const IP = "ip"; |
||
24 | |||
25 | /** |
||
26 | * The hostname string. |
||
27 | * @var string |
||
28 | */ |
||
29 | const HOSTNAME = "hostname"; |
||
30 | |||
31 | /** |
||
32 | * The loc string. |
||
33 | * @var string |
||
34 | */ |
||
35 | const LOC = "loc"; |
||
36 | |||
37 | /** |
||
38 | * The org string. |
||
39 | * @var string |
||
40 | */ |
||
41 | const ORG = "org"; |
||
42 | |||
43 | /** |
||
44 | * The city string. |
||
45 | * @var string |
||
46 | */ |
||
47 | const CITY = "city"; |
||
48 | |||
49 | /** |
||
50 | * The region string. |
||
51 | * @var string |
||
52 | */ |
||
53 | const REGION = "region"; |
||
54 | |||
55 | /** |
||
56 | * The country string. |
||
57 | * @var string |
||
58 | */ |
||
59 | const COUNTRY = "country"; |
||
60 | |||
61 | /** |
||
62 | * The phone string. |
||
63 | * @var string |
||
64 | */ |
||
65 | const PHONE = "phone"; |
||
66 | |||
67 | /** |
||
68 | * The geo string. |
||
69 | * @var string |
||
70 | */ |
||
71 | const GEO = "geo"; |
||
72 | |||
73 | /** |
||
74 | * The postal string. |
||
75 | * @var string |
||
76 | */ |
||
77 | const POSTAL = "postal"; |
||
78 | |||
79 | /** |
||
80 | * All the settings |
||
81 | * @var array |
||
82 | */ |
||
83 | protected $settings; |
||
84 | |||
85 | /** |
||
86 | * Create an Ipinfo instance. |
||
87 | * @param array $settings An array with all the settings. |
||
88 | * Supported keys are: |
||
89 | * - token: string the developer token; |
||
90 | * - debug: boolean active or not the debug. |
||
91 | */ |
||
92 | 8 | public function __construct($settings = array()) |
|
100 | |||
101 | |||
102 | /** |
||
103 | * Get all the info about your own ip address. |
||
104 | * @return \DavidePastore\Ipinfo\Host The Host object with all the info. |
||
105 | */ |
||
106 | 1 | public function getYourOwnIpDetails() |
|
107 | { |
||
108 | 1 | $response = $this->makeCurlRequest($this::BASE_URL . "json"); |
|
109 | 1 | $response = json_decode($response, true); |
|
110 | 1 | return new Host($response); |
|
111 | } |
||
112 | |||
113 | /** |
||
114 | * Get all the info about an ip address. |
||
115 | * @param string $ipAddress The ip address. |
||
116 | * @return \DavidePastore\Ipinfo\Host The Host object with all the info. |
||
117 | */ |
||
118 | 2 | public function getFullIpDetails($ipAddress) |
|
124 | |||
125 | /** |
||
126 | * Get a specific field value. |
||
127 | * @param string $ipAddress The ip address. |
||
128 | * @param string $field The field. |
||
129 | * @return string|\DavidePastore\Ipinfo\Host The value of the given field for the given ip. |
||
130 | * This could returns an Host object if you call it with for the field |
||
131 | * \DavidePastore\Ipinfo\Ipinfo::GEO. |
||
132 | */ |
||
133 | 4 | public function getSpecificField($ipAddress, $field) |
|
139 | |||
140 | /** |
||
141 | * Get a specific field value of your own ip address. |
||
142 | * @param string $field The field. |
||
143 | * @return string|\DavidePastore\Ipinfo\Host The value of the given field for your own ip. |
||
144 | * This could returns an Host object if you call it with for the field |
||
145 | * \DavidePastore\Ipinfo\Ipinfo::GEO. |
||
146 | */ |
||
147 | 1 | public function getYourOwnIpSpecificField($field) |
|
148 | { |
||
149 | 1 | $response = $this->makeCurlRequest($this::BASE_URL . $field); |
|
150 | 1 | $response = $this->checkGeo($field, $response); |
|
151 | 1 | return $response; |
|
152 | } |
||
153 | |||
154 | /** |
||
155 | * Use the /geo call to get just the geolocation information, which will often be |
||
156 | * faster than getting the full response. |
||
157 | * |
||
158 | * @param string $ipAddress The ip address. |
||
159 | * @return \DavidePastore\Ipinfo\Host |
||
160 | */ |
||
161 | 1 | public function getIpGeoDetails($ipAddress) |
|
165 | |||
166 | /** |
||
167 | * Check if the response is GEO and set the parameters accordingly. |
||
168 | * @param string $field The field value. |
||
169 | * @param string $response The response from the server. |
||
170 | * @return Ambigous <\DavidePastore\Ipinfo\Host, string> Returns an Host object if the request is |
||
171 | * of the GEO type, a string otherwise. If the field value is different from the GEO type, it will |
||
172 | * delete the last character ('\n'). |
||
173 | */ |
||
174 | 5 | private function checkGeo($field, $response) |
|
185 | |||
186 | /** |
||
187 | * Make a curl request. |
||
188 | * @param string $address The address of the request. |
||
189 | * @return string Returns the response from the request. |
||
190 | */ |
||
191 | 8 | private function makeCurlRequest($address) |
|
214 | } |
||
215 |