From 16e117425362a0eedd01fddd033c6761b6399945 Mon Sep 17 00:00:00 2001 From: Danny van Kooten Date: Sat, 21 Jan 2017 10:14:57 +0100 Subject: [PATCH] use curl for fetching VAT rates from jsonvat.com, #4 --- src/Rates/Clients/JsonVat.php | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/Rates/Clients/JsonVat.php b/src/Rates/Clients/JsonVat.php index d6b61d8..930f8b0 100644 --- a/src/Rates/Clients/JsonVat.php +++ b/src/Rates/Clients/JsonVat.php @@ -20,12 +20,17 @@ class JsonVat implements Client{ public function fetch() { $url = self::URL; - // fetch data - $response = file_get_contents($url); - if( empty( $response ) ) { + $curl_handle = curl_init(); + curl_setopt($curl_handle, CURLOPT_URL, $url); + curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 0); + curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true); + $response_body = curl_exec($curl_handle); + curl_close($curl_handle); + + if( empty( $response_body ) ) { throw new ClientException( "Error fetching rates from {$url}."); } - $data = json_decode($response); + $data = json_decode($response_body); // build map with country codes => rates $map = array();