diff --git a/CHANGELOG.md b/CHANGELOG.md index aa2f3e5..e63ba5b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # TDE - Geo Address Changelog +## 1.0.16 - 2022-08-17 +### Fixed +- Use curl instead of file_get_contents + + ## 1.0.15 - 2021-10-19 ### Fixed - Fixed a bug where still an array was expected instead of the GeoAddress-model. diff --git a/composer.json b/composer.json index 74c0f7e..7984076 100644 --- a/composer.json +++ b/composer.json @@ -14,8 +14,9 @@ } ], "require": { - "craftcms/cms": "^3.0.0-RC1" - }, + "craftcms/cms": "^3.0.0-RC1", + "ext-curl": "*" + }, "autoload": { "psr-4": { "TDE\\GeoAddress\\": "src/" diff --git a/src/services/GeoAddressService.php b/src/services/GeoAddressService.php index 8176ae0..2c1a9c9 100644 --- a/src/services/GeoAddressService.php +++ b/src/services/GeoAddressService.php @@ -21,12 +21,10 @@ class GeoAddressService extends Component */ public function getCoordsByAddress(string $address, string $country) { - $requestUrl = 'https://maps.googleapis.com/maps/api/geocode/json'; + $requestUrl = 'https://maps.googleapis.com/maps/api/geocode/json'; $requestUrl .= '?address=' . rawurlencode($address); $requestUrl .= '&key=' . GeoAddress::getInstance()->getSettings()->googleApiKey; - $result = json_decode(file_get_contents($requestUrl)); - $address = [ 'lat' => null, 'lng' => null, @@ -35,6 +33,16 @@ public function getCoordsByAddress(string $address, string $country) 'countryCode' => null, ]; + if (!GeoAddress::getInstance()?->getSettings()->googleApiKey) { + return $address; + } + + $ch = curl_init($requestUrl); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); + curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5); + curl_setopt($ch, CURLOPT_TIMEOUT, 3); + $result = json_decode(curl_exec($ch), true, 512, JSON_THROW_ON_ERROR); + // no results if ($result->status !== 'OK' || empty($result->results)) { Craft::warning(