Skip to content
This repository has been archived by the owner on Aug 21, 2024. It is now read-only.

Commit

Permalink
Change file_get_contents to curl
Browse files Browse the repository at this point in the history
  • Loading branch information
jordikroon committed Aug 17, 2022
1 parent 5466c6b commit df32012
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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/"
Expand Down
14 changes: 11 additions & 3 deletions src/services/GeoAddressService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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(
Expand Down

0 comments on commit df32012

Please sign in to comment.