/
Main.php
61 lines (52 loc) · 1.77 KB
/
Main.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<?php
require 'vendor/autoload.php';
use \Forecast\Forecast;
use \AnthonyMartin\GeoLocation\GeoLocation;
use \JeroenDesloovere\Distance\Distance;
class Main
{
const API_ID_FORECAST = '5cc3a94fa51ff43574c767fa262747d1';
private $forecast;
private $currentLocation;
public function __construct($ip)
{
$this->forecast = new Forecast(self::API_ID_FORECAST);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.telize.com/geoip/$ip");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
curl_close($ch);
$this->currentLocation = json_decode($data);
}
public function printCurrentLocation()
{
echo 'You in ' . $this->currentLocation->city . '.<br>';
}
public function printWeather()
{
$cityWeather = $this->forecast->get(
$this->currentLocation->latitude,
$this->currentLocation->longitude,
null,
[
'units' => 'si',
'exclude' => 'flags'
]);
echo 'Temperature in your city ' . $cityWeather->currently->temperature . ' °С<br>';
}
public function printDistances($locations)
{
foreach ($locations as $item) {
$response = GeoLocation::getGeocodeFromGoogle($item);
$latitude = $response->results[0]->geometry->location->lat;
$longitude = $response->results[0]->geometry->location->lng;
$distance = Distance::between(
$latitude,
$longitude,
$this->currentLocation->latitude,
$this->currentLocation->longitude
);
echo "Distance between {$this->currentLocation->city} and $item = " . $distance . "km<br>";
}
}
}