· 6 years ago · Oct 31, 2019, 10:44 AM
1<?php
2
3/*
4 * OpenWeatherMap-PHP-API — A PHP API to parse weather data from https://OpenWeatherMap.org.
5 *
6 * @license MIT
7 *
8 * Please see the LICENSE file distributed with this source code for further
9 * information regarding copyright and licensing.
10 *
11 * Please visit the following links to read about the usage policies and the license of
12 * OpenWeatherMap data before using this library:
13 *
14 * @see https://OpenWeatherMap.org/price
15 * @see https://OpenWeatherMap.org/terms
16 * @see https://OpenWeatherMap.org/appid
17 */
18
19use Cmfcmf\OpenWeatherMap;
20use Cmfcmf\OpenWeatherMap\Exception as OWMException;
21use Tuupola\Http\Factory\RequestFactory;
22use Http\Adapter\Guzzle6\Client as GuzzleAdapter;
23
24require './vendor/autoload.php';
25require_once __DIR__ . '/bootstrap.php';
26
27$httpRequestFactory = new RequestFactory();
28$httpClient = GuzzleAdapter::createWithConfig([]);
29
30$owm = new OpenWeatherMap($myApiKey, $httpClient, $httpRequestFactory);
31
32$cli = false;
33$lf = '<br>';
34// if (php_sapi_name() === 'cli') {
35// $lf = "\n";
36// $cli = true;
37// }
38
39$lang = 'pl';
40// Units (can be 'metric' or 'imperial' [default]):
41$units = 'metric';
42
43// Example 2: Get current pressure and humidity in London.
44$weather = $owm->getWeather('London', $units, $lang);
45echo "$lf$lf EXAMPLE 2$lf";
46
47/*
48 * You can use the methods above to only get the value or the unit.
49 */
50echo 'Last update: '.$weather->lastUpdate->format('r');
51echo $lf;
52echo 'Pressure: '.$weather->pressure;
53echo $lf;
54echo 'Temperature: '.$weather->temperature->now;
55echo $lf;
56echo 'Wind: '.$weather->wind->speed;
57echo $lf;
58echo 'Humidity: '.$weather->humidity;
59echo $lf;
60
61// echo $_GET['city'];
62echo $lf;
63echo $lf;
64
65class MyWeather extends OpenWeatherMap {
66 // private $secretKey = 'askdij1123';
67 // public $requestUrl = 'https://api.openweathermap.org/data/2.5/weather?q=';
68 public $cityNames = array();
69 public $br = '<br>';
70 public $lang = 'pl';
71 public $units = 'metric';
72
73 function __construct($cityNames) {
74 $this->cityNames = explode(",",$cityNames);
75 }
76
77 function get_names() {
78 foreach ($this->cityNames as $name) {
79 echo $name.$this->br;
80 }
81
82 $lang = 'pl';
83 // Units (can be 'metric' or 'imperial' [default]):
84 $units = 'metric';
85
86 // Example 2: Get current pressure and humidity in London.
87 // $owm = new OpenWeatherMap();
88 // $weather = $owm->getWeather('London', $units, $lang);
89 }
90
91 function get_weather() {
92 foreach ($this->cityNames as $name) {
93
94 }
95 }
96}
97
98$weather = new MyWeather($_GET['city']);
99
100echo $weather->get_names();
101// echo $weather->get_weather();