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

Last change on this file since db38696 was db38696, checked in by Joachim Langenbach <joachim.langenbach@…>, 11 years ago
  • Added Location class, to store a city
  • Property mode set to 100644
File size: 2.8 KB
RevLine 
[db38696]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" => 5.0,
49 "http_errors" => false,
50 ]
51 );
52 $response = $client->get("/json/". $ip);
53 if(is_null($response)){
54 $errorMessage = "IpGeoLocator: Failed to fetch ip";
55 return null;
56 }
57 if($response->getStatusCode() != 200){
58 if($response->getStatusCode() == 403){
59 $errorMessage = "IpGeoLocator: Probably too many ". $baseUrl ." requests (>10000/hour). Reason:". $response->getReasonPhrase();
60 }
61 else{
62 $errorMessage = "IpGeoLocator: Request failed with code ". $response->getStatusCode() ." and reason ". $response->getReasonPhrase();
63 }
64 error_log($errorMessage);
65 return null;
66 }
67
68 $body = $response->getBody();
69 if(empty($body)){
70 $errorMessage = "IpGeoLocator: Returned empty body";
71 error_log($errorMessage);
72 return null;
73 }
74 $json = json_decode($body, TRUE);
75 if(is_null($json) || !$json || !is_array($json)){
76 $errorMessage = "IpGeoLocator: Could not convert answer into json data\n". $body;
77 error_log($errorMessage);
78 return null;
79 }
80
81 $location = new Location($json["city"], $json["zip_code"],
82 $json["region_name"], $json["region_code"],
83 $json["country_name"], $json["country_code"],
84 $json["latitude"], $json["longitude"],
85 $json["time_zone"]);
86 return $location;
87 }
88}
89
90?>
Note: See TracBrowser for help on using the repository browser.