From bc676f561f794bff9aeed99eb40f8cc7f7a1c114 Mon Sep 17 00:00:00 2001 From: Danny van Kooten Date: Sat, 21 Jan 2017 10:17:54 +0100 Subject: [PATCH] use CURL for ip geolocation, closes #4 --- src/Countries.php | 13 ++++++++++--- src/Rates/Clients/JsonVat.php | 2 +- tests/CountriesTest.php | 1 + 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/Countries.php b/src/Countries.php index 261bfb2..37edc03 100644 --- a/src/Countries.php +++ b/src/Countries.php @@ -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]; } diff --git a/src/Rates/Clients/JsonVat.php b/src/Rates/Clients/JsonVat.php index 930f8b0..179a70a 100644 --- a/src/Rates/Clients/JsonVat.php +++ b/src/Rates/Clients/JsonVat.php @@ -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); diff --git a/tests/CountriesTest.php b/tests/CountriesTest.php index 82de010..279b685 100644 --- a/tests/CountriesTest.php +++ b/tests/CountriesTest.php @@ -33,4 +33,5 @@ public function test_inEurope() { self::assertTrue( $countries->inEurope( $country ) ); } } + }