3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Weather { /** * service properties */ public $attributes; /** * constant definitions (forecast mode) */ const MARINE = "marine"; const MOUNTAIN = "ski"; const WEATHER = "weather"; /** * constructor for this class * @param string $key the API key for this element * @return mixed an instance of this class * @example $weather = new \Services\Information\Weather(); * @example $weather = new \Services\Information\Weather("sample key"); * @version 1.0 * @author Julio Meca Hansen <jmecahansen@gmail.com> */ public function __construct($key = "ad14d73de30ee62b02aa6022a1b2d") { $this->attributes["key"] = $key; $this->attributes["mode"] = self::WEATHER; return $this; } /** * returns the date (in ISO format) for this element * @return string the date (in ISO format) for this element * @example $date = $weather->getDate(); * @version 1.0 * @author Julio Meca Hansen <jmecahansen@gmail.com> */ public function getDate() { if (!empty($this->attributes["date"])) { return $this->attributes["date"]; } } /** * returns the number of forecasting days for this element * @return numeric the number of forecasting days for this element * @example $days = $weather->getDays(); * @version 1.0 * @author Julio Meca Hansen <jmecahansen@gmail.com> */ public function getDays() { if (!empty($this->attributes["days"])) { return $this->attributes["days"]; } } /** * returns the API key for this element * @return string the API key for this element * @example $key = $weather->getKey(); * @version 1.0 * @author Julio Meca Hansen <jmecahansen@gmail.com> */ public function getKey() { if (!empty($this->attributes["key"])) { return $this->attributes["key"]; } } /** * returns the location for this element * @return mixed the location for this element * @example $location = $weather->getLocation(); * @version 1.0 * @author Julio Meca Hansen <jmecahansen@gmail.com> */ public function getLocation() { if (!empty($this->attributes["location"])) { return $this->attributes["location"]; } } /** * returns the forecasting mode for this element * @return mixed the forecasting mode for this element * @example $mode = $weather->getMode(); * @version 1.0 * @author Julio Meca Hansen <jmecahansen@gmail.com> */ public function getMode() { if (!empty($this->attributes["mode"])) { return $this->attributes["mode"]; } } /** * returns the list of parameters for this element * @return string the list of URL request parameters for this element * @version 1.0 * @author Julio Meca Hansen <jmecahansen@gmail.com> */ public function getParameters() { if (!empty($this->attributes)) { $parameters[] = "format=json"; if (!empty($this->attributes["location"])) { $parameters[] = "q=" . urlencode($this->attributes["location"]); } if (!empty($this->attributes["days"])) { $parameters[] = "num_of_days=" . $this->attributes["days"]; } if (!empty($this->attributes["date"])) { $parameters[] = "date=" . $this->attributes["date"]; } if (!empty($this->attributes["key"])) { $parameters[] = "key=" . $this->attributes["key"]; } return "?" . implode("&", $parameters); } } /** * sets the number of forecasting days for this element * @param numeric $days the number of forecasting days for this element * @example $weather->setDays(3); * @version 1.0 * @author Julio Meca Hansen <jmecahansen@gmail.com> */ public function setDays($days) { if (!empty($days)) { if (is_numeric($days)) { if ($days > 0) { $this->attributes["days"] = $days; } } } return $this; } /** * sets the API key for this element * @param string $key the API key for this element * @example $weather->setKey("sample key"); * @version 1.0 * @author Julio Meca Hansen <jmecahansen@gmail.com> */ public function setKey($key) { if (!empty($key)) { $this->attributes["key"] = $key; } return $this; } /** * sets the location for this element * @param mixed $data the input data for this element (either the latitude/longitude pair or the city name) * @example $weather->setLocation(39.607679, 2.869080); * @example $weather->setLocation("palma"); * @version 1.0 * @author Julio Meca Hansen <jmecahansen@gmail.com> */ public function setLocation() { if (!empty($arguments = func_get_args())) { if (!empty($count = func_num_args())) { if ($count > 1) { if (!empty($latitude = current($arguments))) { if (!empty($longitude = next($arguments))) { if (is_numeric($latitude)) { if (is_numeric($longitude)) { $this->attributes["location"] = [$latitude, $longitude]; } } } } } elseif ($count > 0) { if (!empty($location = current($arguments))) { if (!is_numeric($location)) { $this->attributes["location"] = $location; } } } } } return $this; } /** * sets the forecast mode for this element * @param string $mode the forecast mode for this element * @example $weather->setMode(\Services\Information\Weather::MARINE); * @version 1.0 * @author Julio Meca Hansen <jmecahansen@gmail.com> */ public function setMode($mode) { if (!empty($mode)) { if (in_array($mode, [ self::MARINE, self::MOUNTAIN, self::WEATHER ], true)) { $this->attributes["mode"] = $mode; } } return $this; } /** * outputs this element * @return mixed the weather forecasting data for this element * @example $data = $weather->output(); * @version 1.0 * @author Julio Meca Hansen <jmecahansen@gmail.com> */ public function output() { $output = false; if (!empty($this->attributes["mode"])) { if (in_array($this->attributes["mode"], [ self::MARINE, self::MOUNTAIN, self::WEATHER ], true)) { if (!empty($this->attributes["key"])) { if (!empty($this->attributes["location"])) { if (!empty($this->attributes["days"])) { if (!empty($c = curl_init(\Helpers\URL::getProtocol() . "://api.worldweatheronline.com/free/v2/{$this->attributes["mode"]}.ashx{$this->getParameters()}"))) { curl_setopt($c, CURLOPT_HEADER, false); curl_setopt($c, CURLOPT_RETURNTRANSFER, true); if (!empty($input = json_decode(curl_exec($c), true))) { if (!empty($input["data"])) { $codes = [ 113 => _("soleado"), 116 => _("parcialmente nublado"), 119 => _("nublado"), 122 => _("nublado"), 143 => _("bruma"), 176 => _("lluvia irregular"), 179 => _("nieve irregular"), 182 => _("aguanieve irregular"), 185 => _("congelación irregular"), 200 => _("nublado con posibilidad de tormenta"), 227 => _("nevada con viento ligero"), 230 => _("tormenta de nieve"), 248 => _("niebla"), 260 => _("niebla helada"), 263 => _("nublado con llovizna"), 266 => _("llovizna"), 281 => _("llovizna con helada"), 284 => _("llovizna fuerte con helada"), 293 => _("nublado con lluvia ligera"), 296 => _("lluvia ligera"), 299 => _("lluvia moderada esporádica"), 302 => _("lluvia moderada"), 305 => _("lluvia fuerte esporádica"), 308 => _("lluvia fuerte"), 311 => _("lluvia ligera con helada"), 314 => _("lluvia moderada o fuerte con helada"), 317 => _("aguanieve ligera"), 320 => _("aguanieve moderada o fuerte"), 323 => _("nublado con nevada ligera"), 326 => _("nevada ligera"), 329 => _("nublado con nevada moderada"), 332 => _("nevada moderada"), 335 => _("nublado con nevada fuerte"), 338 => _("nevada fuerte"), 350 => _("granizo"), 353 => _("episodios de lluvia ligera"), 356 => _("episodios de lluvia moderada o fuerte"), 359 => _("lluvia torrencial"), 362 => _("episodios de aguanieve ligera"), 365 => _("episodios de aguanieve moderada o fuerte"), 368 => _("episodios de nieve ligera"), 371 => _("episodios de nieve moderada o fuerte"), 374 => _("episodios de granizo ligera"), 377 => _("episodios de granizo moderada o fuerte"), 386 => _("nublado con lluvia ligera y truenos"), 389 => _("nevada moderada o fuerte con tormenta"), 392 => _("nublado con nevada ligera y truenos"), 395 => _("nevada moderada o fuerte con tormenta") ]; if ($this->attributes["mode"] === self::MARINE) { print_r($input["data"]); } elseif ($this->attributes["mode"] === self::MOUNTAIN) { print_r($input["data"]); } elseif ($this->attributes["mode"] === self::WEATHER) { if (!empty($input["data"]["weather"])) { foreach ($input["data"]["weather"] as $forecast) { $output["forecast"][] = [ "date" => $forecast["date"], "temperature" => [ "minimum" => [ "C" => $forecast["mintempC"], "F" => $forecast["mintempF"], ], "maximum" => [ "C" => $forecast["maxtempC"], "F" => $forecast["maxtempF"], ] ], "details" => array_map(function ($i) use ($codes) { return [ "cloudcover" => $i["cloudcover"], "description" => [ "code" => $i["weatherCode"], "text" => $codes[$i["weatherCode"]] ], "humidity" => $i["humidity"], "precipitation" => $i["precipMM"], "pressure" => $i["pressure"], "temperature" => [ "felt" => [ "C" => $i["FeelsLikeC"], "F" => $i["FeelsLikeF"], ], "measured" => [ "C" => $i["tempC"], "F" => $i["tempF"], ] ], "time" => str_pad($i["time"] / 100, 2, "0", STR_PAD_LEFT) . ":00", "visibility" => $i["visibility"], "wind" => [ "angle" => $i["winddirDegree"], "direction" => $i["winddir16Point"], "speed" => [ "kilometers" => $i["windspeedKmph"], "miles" => $i["windspeedMiles"] ] ] ]; }, $forecast["hourly"]) ]; } } } } } } } } } } } return $output; } } $w = new Weather(); $w->setLocation("Palma"); $w->setDays(3); $w->setMode(Weather::MARINE); print_r($w->output()); ?>
Output for 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
Fatal error: Uncaught Error: Call to undefined function curl_init() in /in/7i86N:234 Stack trace: #0 /in/7i86N(365): Weather->output() #1 {main} thrown in /in/7i86N on line 234
Process exited with code 255.
Output for 5.5.0 - 5.5.38, 5.6.0 - 5.6.40
Fatal error: Call to undefined function curl_init() in /in/7i86N on line 234
Process exited with code 255.
Output for 5.4.0 - 5.4.45
Parse error: syntax error, unexpected '=', expecting ')' in /in/7i86N on line 168
Process exited with code 255.

preferences:
281.43 ms | 401 KiB | 376 Q