Completed
Pull Request — master (#36)
by Davide
05:05 queued 02:34
created
src/DavidePastore/Ipinfo/Ipinfo.php 1 patch
Indentation   +272 added lines, -272 removed lines patch added patch discarded remove patch
@@ -12,276 +12,276 @@
 block discarded – undo
12 12
  */
13 13
 class Ipinfo
14 14
 {
15
-    /**
16
-     * The base url of the ipinfo service.
17
-     *
18
-     * @var string
19
-     */
20
-    const BASE_URL = 'https://ipinfo.io/';
21
-
22
-    /**
23
-     * The ip string.
24
-     *
25
-     * @var string
26
-     */
27
-    const IP = 'ip';
28
-
29
-    /**
30
-     * The hostname string.
31
-     *
32
-     * @var string
33
-     */
34
-    const HOSTNAME = 'hostname';
35
-
36
-    /**
37
-     * The loc string.
38
-     *
39
-     * @var string
40
-     */
41
-    const LOC = 'loc';
42
-
43
-    /**
44
-     * The org string.
45
-     *
46
-     * @var string
47
-     */
48
-    const ORG = 'org';
49
-
50
-    /**
51
-     * The city string.
52
-     *
53
-     * @var string
54
-     */
55
-    const CITY = 'city';
56
-
57
-    /**
58
-     * The region string.
59
-     *
60
-     * @var string
61
-     */
62
-    const REGION = 'region';
63
-
64
-    /**
65
-     * The country string.
66
-     *
67
-     * @var string
68
-     */
69
-    const COUNTRY = 'country';
70
-
71
-    /**
72
-     * The phone string.
73
-     *
74
-     * @var string
75
-     */
76
-    const PHONE = 'phone';
77
-
78
-    /**
79
-     * The geo string.
80
-     *
81
-     * @var string
82
-     */
83
-    const GEO = 'geo';
84
-
85
-    /**
86
-     * The postal string.
87
-     *
88
-     * @var string
89
-     */
90
-    const POSTAL = 'postal';
91
-
92
-    /**
93
-     * All the settings.
94
-     *
95
-     * @var array
96
-     */
97
-    protected $settings;
98
-
99
-    /**
100
-     * Create an Ipinfo instance.
101
-     *
102
-     * @param array $settings An array with all the settings.
103
-     *                        Supported keys are:
104
-     *                        - token: string the developer token;
105
-     *                        - debug: boolean active or not the debug.
106
-     */
107
-    public function __construct($settings = array())
108
-    {
109
-        //Merge user settings
110
-        $this->settings = array_merge(array(
111
-                'token' => '',
112
-                'timeout' => 0,
113
-                'debug' => false,
114
-        ), $settings);
115
-    }
116
-
117
-    /**
118
-     * Get all the info about your own ip address.
119
-     *
120
-     * @return \DavidePastore\Ipinfo\Host The Host object with all the info.
121
-     */
122
-    public function getYourOwnIpDetails()
123
-    {
124
-        $response = $this->makeCurlRequest($this::BASE_URL.'json');
125
-        $response = $this->jsonDecodeResponse($response);
126
-
127
-        return new Host($response);
128
-    }
129
-
130
-    /**
131
-     * Get all the info about an ip address.
132
-     *
133
-     * @param string $ipAddress The ip address.
134
-     *
135
-     * @return \DavidePastore\Ipinfo\Host The Host object with all the info.
136
-     */
137
-    public function getFullIpDetails($ipAddress)
138
-    {
139
-        $response = $this->makeCurlRequest($this::BASE_URL.$ipAddress);
140
-        $response = $this->jsonDecodeResponse($response);
141
-
142
-        return new Host($response);
143
-    }
144
-
145
-    /**
146
-     * Get a specific field value.
147
-     *
148
-     * @param string $ipAddress The ip address.
149
-     * @param string $field     The field.
150
-     *
151
-     * @return string|\DavidePastore\Ipinfo\Host The value of the given field for the given ip.
152
-     *                                           This could returns an Host object if you call it with for the field
153
-     *                                           \DavidePastore\Ipinfo\Ipinfo::GEO.
154
-     */
155
-    public function getSpecificField($ipAddress, $field)
156
-    {
157
-        $response = $this->makeCurlRequest($this::BASE_URL.$ipAddress.'/'.$field);
158
-        $response = $this->checkGeo($field, $response);
159
-
160
-        return $response;
161
-    }
162
-
163
-    /**
164
-     * Get a specific field value of your own ip address.
165
-     *
166
-     * @param string $field The field.
167
-     *
168
-     * @return string|\DavidePastore\Ipinfo\Host The value of the given field for your own ip.
169
-     *                                           This could returns an Host object if you call it with for the field
170
-     *                                           \DavidePastore\Ipinfo\Ipinfo::GEO.
171
-     */
172
-    public function getYourOwnIpSpecificField($field)
173
-    {
174
-        $response = $this->makeCurlRequest($this::BASE_URL.$field);
175
-        $response = $this->checkGeo($field, $response);
176
-
177
-        return $response;
178
-    }
179
-
180
-    /**
181
-     * Use the /geo call to get just the geolocation information, which will often be
182
-     * faster than getting the full response.
183
-     *
184
-     * @param string $ipAddress The ip address.
185
-     *
186
-     * @return \DavidePastore\Ipinfo\Host
187
-     */
188
-    public function getIpGeoDetails($ipAddress)
189
-    {
190
-        return $this->getSpecificField($ipAddress, $this::GEO);
191
-    }
192
-
193
-    /**
194
-     * Check if the response is GEO and set the parameters accordingly.
195
-     *
196
-     * @param string $field    The field value.
197
-     * @param string $response The response from the server.
198
-     *
199
-     * @return Ambigous <\DavidePastore\Ipinfo\Host, string> Returns an Host object if the request is
200
-     *                  of the GEO type, a string otherwise. If the field value is different from the GEO type, it will
201
-     *                  delete the last character ('\n').
202
-     */
203
-    private function checkGeo($field, $response)
204
-    {
205
-        if ($field == $this::GEO) {
206
-            $response = $this->jsonDecodeResponse($response);
207
-            $response = new Host($response);
208
-        } else {
209
-            $this->checkErrors($response);
210
-            $response = substr($response, 0, -1);
211
-        }
212
-
213
-        return $response;
214
-    }
215
-
216
-    /**
217
-     * Make a curl request.
218
-     *
219
-     * @param string $address The address of the request.
220
-     *
221
-     * @return string Returns the response from the request.
222
-     */
223
-    private function makeCurlRequest($address)
224
-    {
225
-        $curl = curl_init();
226
-
227
-        if (!empty($this->settings['token'])) {
228
-            $address .= '?token='.$this->settings['token'];
229
-        }
230
-
231
-        if ($this->settings['debug']) {
232
-            echo 'Request address: '.$address."\n";
233
-        }
234
-
235
-        curl_setopt_array($curl, array(
236
-            CURLOPT_RETURNTRANSFER => 1,
237
-            CURLOPT_URL => $address,
238
-            CURLOPT_CONNECTTIMEOUT => $this->settings['timeout'],
239
-            CURLOPT_CAINFO => __DIR__ . "/cacert.pem"
240
-        ));
241
-
242
-        $response = curl_exec($curl);
243
-
244
-        if ($response === false && $this->settings['debug']) {
245
-            $error = curl_error($curl);
246
-            echo "The error is".$error;
247
-        }
248
-
249
-        curl_close($curl);
250
-
251
-        return $response;
252
-    }
253
-
254
-    /**
255
-     * Returns the json decoded associative array.
256
-     * @param  string $response Response from the http call.
257
-     * @return array           Returns the associative array with the response.
258
-     * @throws RateLimitExceedException    If you exceed the rate limit.
259
-     * @throws InvalidTokenException If the used token is invalid.
260
-     */
261
-    private function jsonDecodeResponse($response)
262
-    {
263
-        if ($response) {
264
-            // Check if the response contains an error message
265
-            $this->checkErrors($response);
266
-            $response = json_decode($response, true);
267
-        } else {
268
-            $response = array();
269
-        }
270
-        return $response;
271
-    }
272
-
273
-    /**
274
-     * Check if the given response has some kind of errors.
275
-     * @param string $response The response to check.
276
-     * @throws RateLimitExceedException    If you exceed the rate limit.
277
-     * @throws InvalidTokenException If the used token is invalid.
278
-     */
279
-    private function checkErrors($response)
280
-    {
281
-        if (strpos($response, 'Rate limit exceeded.') !== false) {
282
-            throw new RateLimitExceedException("You exceed the rate limit.", $response);
283
-        } elseif (strpos($response, 'Unknown token.') !== false) {
284
-            throw new InvalidTokenException("The used token is invalid.", $response);
285
-        }
286
-    }
15
+	/**
16
+	 * The base url of the ipinfo service.
17
+	 *
18
+	 * @var string
19
+	 */
20
+	const BASE_URL = 'https://ipinfo.io/';
21
+
22
+	/**
23
+	 * The ip string.
24
+	 *
25
+	 * @var string
26
+	 */
27
+	const IP = 'ip';
28
+
29
+	/**
30
+	 * The hostname string.
31
+	 *
32
+	 * @var string
33
+	 */
34
+	const HOSTNAME = 'hostname';
35
+
36
+	/**
37
+	 * The loc string.
38
+	 *
39
+	 * @var string
40
+	 */
41
+	const LOC = 'loc';
42
+
43
+	/**
44
+	 * The org string.
45
+	 *
46
+	 * @var string
47
+	 */
48
+	const ORG = 'org';
49
+
50
+	/**
51
+	 * The city string.
52
+	 *
53
+	 * @var string
54
+	 */
55
+	const CITY = 'city';
56
+
57
+	/**
58
+	 * The region string.
59
+	 *
60
+	 * @var string
61
+	 */
62
+	const REGION = 'region';
63
+
64
+	/**
65
+	 * The country string.
66
+	 *
67
+	 * @var string
68
+	 */
69
+	const COUNTRY = 'country';
70
+
71
+	/**
72
+	 * The phone string.
73
+	 *
74
+	 * @var string
75
+	 */
76
+	const PHONE = 'phone';
77
+
78
+	/**
79
+	 * The geo string.
80
+	 *
81
+	 * @var string
82
+	 */
83
+	const GEO = 'geo';
84
+
85
+	/**
86
+	 * The postal string.
87
+	 *
88
+	 * @var string
89
+	 */
90
+	const POSTAL = 'postal';
91
+
92
+	/**
93
+	 * All the settings.
94
+	 *
95
+	 * @var array
96
+	 */
97
+	protected $settings;
98
+
99
+	/**
100
+	 * Create an Ipinfo instance.
101
+	 *
102
+	 * @param array $settings An array with all the settings.
103
+	 *                        Supported keys are:
104
+	 *                        - token: string the developer token;
105
+	 *                        - debug: boolean active or not the debug.
106
+	 */
107
+	public function __construct($settings = array())
108
+	{
109
+		//Merge user settings
110
+		$this->settings = array_merge(array(
111
+				'token' => '',
112
+				'timeout' => 0,
113
+				'debug' => false,
114
+		), $settings);
115
+	}
116
+
117
+	/**
118
+	 * Get all the info about your own ip address.
119
+	 *
120
+	 * @return \DavidePastore\Ipinfo\Host The Host object with all the info.
121
+	 */
122
+	public function getYourOwnIpDetails()
123
+	{
124
+		$response = $this->makeCurlRequest($this::BASE_URL.'json');
125
+		$response = $this->jsonDecodeResponse($response);
126
+
127
+		return new Host($response);
128
+	}
129
+
130
+	/**
131
+	 * Get all the info about an ip address.
132
+	 *
133
+	 * @param string $ipAddress The ip address.
134
+	 *
135
+	 * @return \DavidePastore\Ipinfo\Host The Host object with all the info.
136
+	 */
137
+	public function getFullIpDetails($ipAddress)
138
+	{
139
+		$response = $this->makeCurlRequest($this::BASE_URL.$ipAddress);
140
+		$response = $this->jsonDecodeResponse($response);
141
+
142
+		return new Host($response);
143
+	}
144
+
145
+	/**
146
+	 * Get a specific field value.
147
+	 *
148
+	 * @param string $ipAddress The ip address.
149
+	 * @param string $field     The field.
150
+	 *
151
+	 * @return string|\DavidePastore\Ipinfo\Host The value of the given field for the given ip.
152
+	 *                                           This could returns an Host object if you call it with for the field
153
+	 *                                           \DavidePastore\Ipinfo\Ipinfo::GEO.
154
+	 */
155
+	public function getSpecificField($ipAddress, $field)
156
+	{
157
+		$response = $this->makeCurlRequest($this::BASE_URL.$ipAddress.'/'.$field);
158
+		$response = $this->checkGeo($field, $response);
159
+
160
+		return $response;
161
+	}
162
+
163
+	/**
164
+	 * Get a specific field value of your own ip address.
165
+	 *
166
+	 * @param string $field The field.
167
+	 *
168
+	 * @return string|\DavidePastore\Ipinfo\Host The value of the given field for your own ip.
169
+	 *                                           This could returns an Host object if you call it with for the field
170
+	 *                                           \DavidePastore\Ipinfo\Ipinfo::GEO.
171
+	 */
172
+	public function getYourOwnIpSpecificField($field)
173
+	{
174
+		$response = $this->makeCurlRequest($this::BASE_URL.$field);
175
+		$response = $this->checkGeo($field, $response);
176
+
177
+		return $response;
178
+	}
179
+
180
+	/**
181
+	 * Use the /geo call to get just the geolocation information, which will often be
182
+	 * faster than getting the full response.
183
+	 *
184
+	 * @param string $ipAddress The ip address.
185
+	 *
186
+	 * @return \DavidePastore\Ipinfo\Host
187
+	 */
188
+	public function getIpGeoDetails($ipAddress)
189
+	{
190
+		return $this->getSpecificField($ipAddress, $this::GEO);
191
+	}
192
+
193
+	/**
194
+	 * Check if the response is GEO and set the parameters accordingly.
195
+	 *
196
+	 * @param string $field    The field value.
197
+	 * @param string $response The response from the server.
198
+	 *
199
+	 * @return Ambigous <\DavidePastore\Ipinfo\Host, string> Returns an Host object if the request is
200
+	 *                  of the GEO type, a string otherwise. If the field value is different from the GEO type, it will
201
+	 *                  delete the last character ('\n').
202
+	 */
203
+	private function checkGeo($field, $response)
204
+	{
205
+		if ($field == $this::GEO) {
206
+			$response = $this->jsonDecodeResponse($response);
207
+			$response = new Host($response);
208
+		} else {
209
+			$this->checkErrors($response);
210
+			$response = substr($response, 0, -1);
211
+		}
212
+
213
+		return $response;
214
+	}
215
+
216
+	/**
217
+	 * Make a curl request.
218
+	 *
219
+	 * @param string $address The address of the request.
220
+	 *
221
+	 * @return string Returns the response from the request.
222
+	 */
223
+	private function makeCurlRequest($address)
224
+	{
225
+		$curl = curl_init();
226
+
227
+		if (!empty($this->settings['token'])) {
228
+			$address .= '?token='.$this->settings['token'];
229
+		}
230
+
231
+		if ($this->settings['debug']) {
232
+			echo 'Request address: '.$address."\n";
233
+		}
234
+
235
+		curl_setopt_array($curl, array(
236
+			CURLOPT_RETURNTRANSFER => 1,
237
+			CURLOPT_URL => $address,
238
+			CURLOPT_CONNECTTIMEOUT => $this->settings['timeout'],
239
+			CURLOPT_CAINFO => __DIR__ . "/cacert.pem"
240
+		));
241
+
242
+		$response = curl_exec($curl);
243
+
244
+		if ($response === false && $this->settings['debug']) {
245
+			$error = curl_error($curl);
246
+			echo "The error is".$error;
247
+		}
248
+
249
+		curl_close($curl);
250
+
251
+		return $response;
252
+	}
253
+
254
+	/**
255
+	 * Returns the json decoded associative array.
256
+	 * @param  string $response Response from the http call.
257
+	 * @return array           Returns the associative array with the response.
258
+	 * @throws RateLimitExceedException    If you exceed the rate limit.
259
+	 * @throws InvalidTokenException If the used token is invalid.
260
+	 */
261
+	private function jsonDecodeResponse($response)
262
+	{
263
+		if ($response) {
264
+			// Check if the response contains an error message
265
+			$this->checkErrors($response);
266
+			$response = json_decode($response, true);
267
+		} else {
268
+			$response = array();
269
+		}
270
+		return $response;
271
+	}
272
+
273
+	/**
274
+	 * Check if the given response has some kind of errors.
275
+	 * @param string $response The response to check.
276
+	 * @throws RateLimitExceedException    If you exceed the rate limit.
277
+	 * @throws InvalidTokenException If the used token is invalid.
278
+	 */
279
+	private function checkErrors($response)
280
+	{
281
+		if (strpos($response, 'Rate limit exceeded.') !== false) {
282
+			throw new RateLimitExceedException("You exceed the rate limit.", $response);
283
+		} elseif (strpos($response, 'Unknown token.') !== false) {
284
+			throw new InvalidTokenException("The used token is invalid.", $response);
285
+		}
286
+	}
287 287
 }
Please login to merge, or discard this patch.