close Warning: AdminModule failed with TracError: Unable to instantiate component <class 'trac.ticket.admin.TicketTypeAdminPanel'> (super(type, obj): obj (type TicketTypeAdminPanel) is not an instance or subtype of type (Markup).)

source: src/webservice/htdocs/SiWeather/Webservice/IpGeoLocator.php@ a0f5395

Last change on this file since a0f5395 was b757e0e, checked in by Joachim Langenbach <joachim.langenbach@…>, 11 years ago

IpGeoLocator fetches timeout exception now

  • Property mode set to 100644
File size: 3.1 KB
Line 
1<?php
2/*
3 Copyright (C) 2015 EngSaS - Engineering Solutions and Services Langenbach. All rights reserved.
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18 MA 02110-1301 USA.
19*/
20
21namespace SiWeather\Webservice;
22
23/**
24 * @brief Translates an IP Address into City, Country, Region, Zipcode, Latitude, Longitude, Time/Zone
25 *
26 * Returns null if someting failed.
27 */
28class IpGeoLocator
29{
30 public static function locate($ip, &$errorMessage){
31 if(!$ip || empty($ip)){
32 $errorMessage = "IpGeoLocator: No ip given";
33 return null;
34 }
35
36 $config = new WebserviceConfig();
37 $baseUrl = $config->getConfig("geoipservice");
38 if(!$baseUrl || empty($baseUrl)){
39 $errorMessage = "IpGeoLocator: No geo ip service base url set (see config \"geoipservice\")";
40 error_log($errorMessage);
41 return null;
42 }
43
44 $client = new \GuzzleHttp\Client
45 (
46 [
47 "base_uri" => $baseUrl,
48 "timeout" => 1.0, # we need a very small timeout here, because otherwise the request of our caller function may time out!
49 "http_errors" => false,
50 ]
51 );
52 try{
53 $response = $client->get("/json/". $ip);
54 }
55 catch(\GuzzleHttp\Exception\ConnectException $exception){
56 $errorMessage = "IpGeoLocator: Fetched connect exception (mostly timeout): ". $exception->getMessage();
57 error_log($errorMessage);
58 return null;
59 }
60 if(is_null($response)){
61 $errorMessage = "IpGeoLocator: Failed to fetch ip";
62 error_log($errorMessage);
63 return null;
64 }
65 if($response->getStatusCode() != 200){
66 if($response->getStatusCode() == 403){
67 $errorMessage = "IpGeoLocator: Probably too many ". $baseUrl ." requests (>10000/hour). Reason:". $response->getReasonPhrase();
68 }
69 else{
70 $errorMessage = "IpGeoLocator: Request failed with code ". $response->getStatusCode() ." and reason ". $response->getReasonPhrase();
71 }
72 error_log($errorMessage);
73 return null;
74 }
75
76 $body = $response->getBody();
77 if(empty($body)){
78 $errorMessage = "IpGeoLocator: Returned empty body";
79 error_log($errorMessage);
80 return null;
81 }
82 $json = json_decode($body, TRUE);
83 if(is_null($json) || !$json || !is_array($json)){
84 $errorMessage = "IpGeoLocator: Could not convert answer into json data\n". $body;
85 error_log($errorMessage);
86 return null;
87 }
88
89 $location = new Location($json["city"], $json["zip_code"],
90 $json["region_name"], $json["region_code"],
91 $json["country_name"], $json["country_code"],
92 $json["latitude"], $json["longitude"],
93 $json["time_zone"]);
94 return $location;
95 }
96}
97
98?>
Note: See TracBrowser for help on using the repository browser.