Skip to content

Commit

Permalink
[#14] merged change to Guzzle (by @reswild)
Browse files Browse the repository at this point in the history
  • Loading branch information
bjendres committed Aug 12, 2020
2 parents 04ca9b8 + 0d23a06 commit 100da2f
Showing 1 changed file with 7 additions and 20 deletions.
27 changes: 7 additions & 20 deletions CRM/Utils/Geocode/OpenStreetMapCoding.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,36 +135,23 @@ static function format(&$values, $stateName = FALSE) {
$url .= '&' . urlencode($key) . '=' . urlencode($value);
}

$result = $request = null;
try {
require_once 'HTTP/Request.php';
$request = new HTTP_Request($url);
$result = $request->sendRequest();
} catch(Exception $e) {
// this could happen if there is a connection error or the server shut you out
// see: https://github.com/systopia/de.systopia.osm/issues/8
CRM_Core_Error::debug_log_message('Geocoding failed: ' . $e->getMessage());
return FALSE;
}
$client = new GuzzleHttp\Client();
$request = $client->request('GET', $url);

// check if request was successful
if (PEAR::isError($result)) {
CRM_Core_Error::debug_log_message('Geocoding failed: ' . $result->getMessage());
return FALSE;
}
if ($request->getResponseCode() != 200) {
CRM_Core_Error::debug_log_message('Geocoding failed, invalid response code ' . $request->getResponseCode());
if ($request->getResponseCode() == 429) {
if ($request->getStatusCode() != 200) {
CRM_Core_Error::debug_log_message('Geocoding failed, invalid response code ' . $request->getStatusCode());
if ($request->getStatusCode() == 429) {
// provider says 'TOO MANY REQUESTS'
$values['geo_code_error'] = 'OVER_QUERY_LIMIT';
} else {
$values['geo_code_error'] = $request->getResponseCode();
$values['geo_code_error'] = $request->getStatusCode();
}
return FALSE;
}

// process results
$string = $request->getResponseBody();
$string = $request->getBody();
$json = json_decode($string);

if (is_null($json) || !is_array($json)) {
Expand Down

0 comments on commit 100da2f

Please sign in to comment.