getConfig("geoipservice"); if(!$baseUrl || empty($baseUrl)){ $errorMessage = "IpGeoLocator: No geo ip service base url set (see config \"geoipservice\")"; error_log($errorMessage); return null; } $client = new \GuzzleHttp\Client ( [ "base_uri" => $baseUrl, "timeout" => 1.0, # we need a very small timeout here, because otherwise the request of our caller function may time out! "http_errors" => false, ] ); try{ $response = $client->get("/json/". $ip); } catch(\GuzzleHttp\Exception\ConnectException $exception){ $errorMessage = "IpGeoLocator: Fetched connect exception (mostly timeout): ". $exception->getMessage(); error_log($errorMessage); return null; } if(is_null($response)){ $errorMessage = "IpGeoLocator: Failed to fetch ip"; error_log($errorMessage); return null; } if($response->getStatusCode() != 200){ if($response->getStatusCode() == 403){ $errorMessage = "IpGeoLocator: Probably too many ". $baseUrl ." requests (>10000/hour). Reason:". $response->getReasonPhrase(); } else{ $errorMessage = "IpGeoLocator: Request failed with code ". $response->getStatusCode() ." and reason ". $response->getReasonPhrase(); } error_log($errorMessage); return null; } $body = $response->getBody(); if(empty($body)){ $errorMessage = "IpGeoLocator: Returned empty body"; error_log($errorMessage); return null; } $json = json_decode($body, TRUE); if(is_null($json) || !$json || !is_array($json)){ $errorMessage = "IpGeoLocator: Could not convert answer into json data\n". $body; error_log($errorMessage); return null; } $location = new Location($json["city"], $json["zip_code"], $json["region_name"], $json["region_code"], $json["country_name"], $json["country_code"], $json["latitude"], $json["longitude"], $json["time_zone"]); return $location; } } ?>