Skip to content

Commit

Permalink
use CURL for ip geolocation, closes #4
Browse files Browse the repository at this point in the history
  • Loading branch information
dannyvankooten committed Jan 21, 2017
1 parent 16e1174 commit bc676f5
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
13 changes: 10 additions & 3 deletions src/Countries.php
Original file line number Diff line number Diff line change
Expand Up @@ -349,10 +349,17 @@ public function inEurope($code) {
* @return string
*/
public function ip($ip) {
$response = file_get_contents('http://ip2c.org/' . $ip);
$url = 'http://ip2c.org/' . $ip;

if(!empty($response)) {
$parts = explode( ';', $response );
$curl_handle = curl_init();
curl_setopt($curl_handle, CURLOPT_URL, $url);
curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true);
$response_body = curl_exec($curl_handle);
curl_close($curl_handle);

if(!empty($response_body)) {
$parts = explode( ';', $response_body );
return $parts[1] === 'ZZ' ? '' : $parts[1];
}

Expand Down
2 changes: 1 addition & 1 deletion src/Rates/Clients/JsonVat.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function fetch() {

$curl_handle = curl_init();
curl_setopt($curl_handle, CURLOPT_URL, $url);
curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 0);
curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true);
$response_body = curl_exec($curl_handle);
curl_close($curl_handle);
Expand Down
1 change: 1 addition & 0 deletions tests/CountriesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,5 @@ public function test_inEurope() {
self::assertTrue( $countries->inEurope( $country ) );
}
}

}

0 comments on commit bc676f5

Please sign in to comment.