From fbafa0d3eda56e2abd95bc32e7c5dcf3477a49a9 Mon Sep 17 00:00:00 2001 From: Leonard Fischer Date: Tue, 31 Jan 2017 22:40:24 +0100 Subject: [PATCH] Bugfix and a new query method --- src/API.php | 30 ++++++++++++++++++++++++------ src/Request.php | 9 +++++---- 2 files changed, 29 insertions(+), 10 deletions(-) diff --git a/src/API.php b/src/API.php index ce485a3..2d16fe9 100644 --- a/src/API.php +++ b/src/API.php @@ -10,10 +10,12 @@ class API extends Request { /** - * Retrieves weather data by a (API internal) "city ID". + * Retrieves weather data by a weather station ID. + * See https://www.wunderground.com/wundermap/ for more information * * @param string $id * @return array + * @throws \ErrorException */ public function getByPWSId ($id) { @@ -22,10 +24,11 @@ public function getByPWSId ($id) /** - * Retrieves weather data by a (API internal) "city ID". + * Retrieves weather data by a airport code. * * @param string $code * @return array + * @throws \ErrorException */ public function getByAirportCode ($code) { @@ -34,11 +37,12 @@ public function getByAirportCode ($code) /** - * Retrieves weather data by given coordinates. + * Retrieves weather data by geo coordinates. * * @param float $lat * @param float $lng * @return array + * @throws \ErrorException */ public function getByCoordinates ($lat, $lng) { @@ -47,14 +51,28 @@ public function getByCoordinates ($lat, $lng) /** - * Retrieves weather data by given coordinates. + * Retrieves weather data by a given country and city name. * - * @param string $city * @param string $country + * @param string $city * @return array + * @throws \ErrorException */ - public function getByLocation ($city, $country) + public function getByLocation ($country, $city) { return $this->fetch(['query' => $country . '/' . $city])->getResponseArray(); } // function + + + /** + * Retrieves weather data by a given country and city name. + * + * @param string $zipcode + * @return array + * @throws \ErrorException + */ + public function getByUSZipcode ($zipcode) + { + return $this->fetch(['query' => $zipcode])->getResponseArray(); + } // function } // class \ No newline at end of file diff --git a/src/Request.php b/src/Request.php index 12ecce8..60b66fc 100644 --- a/src/Request.php +++ b/src/Request.php @@ -127,6 +127,7 @@ public function setQuery ($query) * * @param array $parameters * @return $this + * @throws \ErrorException */ public function fetch (array $parameters = []) { @@ -153,21 +154,21 @@ public function fetch (array $parameters = []) ]); $this->responseJSON = file_get_contents($url); - $this->responseArray = json_decode($this->response, true); + $this->responseArray = json_decode($this->responseJSON, true); if (!is_array($this->responseArray)) { - // @todo Throw exception. + throw new \ErrorException('The Weather Underground API response returned no valid JSON: ' . $this->responseJSON); } // if if (!isset($this->responseArray['response'])) { - // @todo Throw exception. + throw new \ErrorException('The Weather Underground API response is not set or empty: ' . $this->responseJSON); } // if if (isset($this->responseArray['response']) && isset($this->responseArray['response']['error'])) { - // @todo Throw exception. + throw new \ErrorException('The Weather Underground API responded with errors: ' . var_export($this->responseArray['response']['error'], true)); } // if return $this;