forked from avargas/dogeweather
-
Notifications
You must be signed in to change notification settings - Fork 0
/
weather.php
43 lines (36 loc) · 1.17 KB
/
weather.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<?php
function get_client_ip() {
$ipaddress = '';
if (getenv('HTTP_CLIENT_IP'))
$ipaddress = getenv('HTTP_CLIENT_IP');
else if(getenv('HTTP_X_FORWARDED_FOR'))
$ipaddress = getenv('HTTP_X_FORWARDED_FOR');
else if(getenv('HTTP_X_FORWARDED'))
$ipaddress = getenv('HTTP_X_FORWARDED');
else if(getenv('HTTP_FORWARDED_FOR'))
$ipaddress = getenv('HTTP_FORWARDED_FOR');
else if(getenv('HTTP_FORWARDED'))
$ipaddress = getenv('HTTP_FORWARDED');
else if(getenv('REMOTE_ADDR'))
$ipaddress = getenv('REMOTE_ADDR');
else
$ipaddress = 'UNKNOWN';
return $ipaddress;
}
$ip = get_client_ip(); // the IP address to query
//test ip
//$ip = "180.76.6.19";
$query = @unserialize(file_get_contents('http://ip-api.com/php/'.$ip));
if($query && $query['status'] == 'success') {
//get Coords
$lat = $query['lat'];
$lon = $query['lon'];
$url = "http://api.openweathermap.org/data/2.5/weather?lat={$lat}&lon={$lon}";
$djson = file_get_contents($url);
echo $djson;
} else {
$url = "http://api.openweathermap.org/data/2.5/weather?q=Paris";
$djson = file_get_contents($url);
echo $djson;
}
?>