@@ -81,9 +81,17 @@ class OpenWeatherMap
8181
8282 private $ fetcher ;
8383
84+ /**
85+ * @var $apiKey.
86+ */
87+
88+ private $ apiKey = '' ;
89+
8490 /**
8591 * Constructs the OpenWeatherMap object.
8692 *
93+ * @param null|string $appid The API key. Defaults to null.
94+ *
8795 * @param null|FetcherInterface $fetcher The interface to fetch the data from OpenWeatherMap. Defaults to
8896 * CurlFetcher() if cURL is available. Otherwise defaults to
8997 * FileGetContentsFetcher() using 'file_get_contents()'.
@@ -94,7 +102,7 @@ class OpenWeatherMap
94102 * @throws \Exception If $cache is neither false nor a valid callable extending Cmfcmf\OpenWeatherMap\Util\Cache.
95103 * @api
96104 */
97- public function __construct ($ fetcher = null , $ cacheClass = false , $ seconds = 600 )
105+ public function __construct ($ appid = null , $ fetcher = null , $ cacheClass = false , $ seconds = 600 )
98106 {
99107 if ($ cacheClass !== false && !($ cacheClass instanceof AbstractCache)) {
100108 throw new \Exception ("The cache class must implement the FetcherInterface! " );
@@ -109,11 +117,35 @@ public function __construct($fetcher = null, $cacheClass = false, $seconds = 600
109117 $ cacheClass = false ;
110118 }
111119
120+ if (!is_null ($ appid )) {
121+ $ this ->apiKey = $ appid ;
122+ }
112123 $ this ->cacheClass = $ cacheClass ;
113124 $ this ->seconds = $ seconds ;
114125 $ this ->fetcher = $ fetcher ;
115126 }
116127
128+ /**
129+ * Sets the API Key
130+ * @param string API key for the OpenWeatherMap account making the connection.
131+ *
132+ * @api
133+ */
134+ public function setApiKey ($ appid )
135+ {
136+ $ this ->apiKey = $ appid ;
137+ }
138+
139+ /**
140+ * Returns the API Key
141+ *
142+ * @api
143+ */
144+ public function getApiKey ()
145+ {
146+ return $ this ->apiKey ;
147+ }
148+
117149 /**
118150 * Returns the current weather at the place you specified as an object.
119151 *
@@ -159,7 +191,7 @@ public function getWeather($query, $units = 'imperial', $lang = 'en', $appid = '
159191 libxml_use_internal_errors (true );
160192 libxml_clear_errors ();
161193
162- $ answer = $ this ->getRawWeatherData ($ query , $ units , $ lang , $ appid , 'xml ' );
194+ $ answer = $ this ->getRawWeatherData ($ query , $ units , $ lang , empty ( $ appid ) ? $ this -> apiKey : $ appid , 'xml ' );
163195
164196 try {
165197 $ xml = new \SimpleXMLElement ($ answer );
@@ -224,9 +256,9 @@ public function getWeatherForecast($query, $units = 'imperial', $lang = 'en', $a
224256 libxml_clear_errors ();
225257
226258 if ($ days <= 5 ) {
227- $ answer = $ this ->getRawHourlyForecastData ($ query , $ units , $ lang , $ appid , 'xml ' );
259+ $ answer = $ this ->getRawHourlyForecastData ($ query , $ units , $ lang , empty ( $ appid ) ? $ this -> apiKey : $ appid , 'xml ' );
228260 } elseif ($ days <= 16 ) {
229- $ answer = $ this ->getRawDailyForecastData ($ query , $ units , $ lang , $ appid , 'xml ' , $ days );
261+ $ answer = $ this ->getRawDailyForecastData ($ query , $ units , $ lang , empty ( $ appid ) ? $ this -> apiKey : $ appid , 'xml ' , $ days );
230262 } else {
231263 throw new \InvalidArgumentException ('Error: forecasts are only available for the next 16 days. $days must be lower than 17. ' );
232264 }
@@ -295,7 +327,7 @@ public function getWeatherHistory($query, \DateTime $start, $endOrCount = 1, $ty
295327 throw new \InvalidArgumentException ('$type must be either "tick", "hour" or "day" ' );
296328 }
297329
298- $ xml = json_decode ($ this ->getRawWeatherHistory ($ query , $ start , $ endOrCount , $ type , $ units , $ lang , $ appid ), true );
330+ $ xml = json_decode ($ this ->getRawWeatherHistory ($ query , $ start , $ endOrCount , $ type , $ units , $ lang , empty ( $ appid ) ? $ this -> apiKey : $ appid ), true );
299331
300332 if ($ xml ['cod ' ] != 200 ) {
301333 throw new OWMException ($ xml ['message ' ], $ xml ['cod ' ]);
@@ -309,7 +341,7 @@ public function getWeatherHistory($query, \DateTime $start, $endOrCount = 1, $ty
309341 */
310342 public function getRawData ($ query , $ units = 'imperial ' , $ lang = 'en ' , $ appid = '' , $ mode = 'xml ' )
311343 {
312- return $ this ->getRawWeatherData ($ query , $ units , $ lang , $ appid , $ mode );
344+ return $ this ->getRawWeatherData ($ query , $ units , $ lang , empty ( $ appid ) ? $ this -> apiKey : $ appid , $ mode );
313345 }
314346
315347 /**
@@ -353,7 +385,7 @@ public function getRawData($query, $units = 'imperial', $lang = 'en', $appid = '
353385 */
354386 public function getRawWeatherData ($ query , $ units = 'imperial ' , $ lang = 'en ' , $ appid = '' , $ mode = 'xml ' )
355387 {
356- $ url = $ this ->buildUrl ($ query , $ units , $ lang , $ appid , $ mode , $ this ->weatherUrl );
388+ $ url = $ this ->buildUrl ($ query , $ units , $ lang , empty ( $ appid ) ? $ this -> apiKey : $ appid , $ mode , $ this ->weatherUrl );
357389
358390 return $ this ->cacheOrFetchResult ($ url );
359391 }
@@ -399,7 +431,7 @@ public function getRawWeatherData($query, $units = 'imperial', $lang = 'en', $ap
399431 */
400432 public function getRawHourlyForecastData ($ query , $ units = 'imperial ' , $ lang = 'en ' , $ appid = '' , $ mode = 'xml ' )
401433 {
402- $ url = $ this ->buildUrl ($ query , $ units , $ lang , $ appid , $ mode , $ this ->weatherHourlyForecastUrl );
434+ $ url = $ this ->buildUrl ($ query , $ units , $ lang , empty ( $ appid ) ? $ this -> apiKey : $ appid , $ mode , $ this ->weatherHourlyForecastUrl );
403435
404436 return $ this ->cacheOrFetchResult ($ url );
405437 }
@@ -450,7 +482,7 @@ public function getRawDailyForecastData($query, $units = 'imperial', $lang = 'en
450482 if ($ cnt > 16 ) {
451483 throw new \InvalidArgumentException ('$cnt must be 16 or below! ' );
452484 }
453- $ url = $ this ->buildUrl ($ query , $ units , $ lang , $ appid , $ mode , $ this ->weatherDailyForecastUrl ) . "&cnt= $ cnt " ;
485+ $ url = $ this ->buildUrl ($ query , $ units , $ lang , empty ( $ appid ) ? $ this -> apiKey : $ appid , $ mode , $ this ->weatherDailyForecastUrl ) . "&cnt= $ cnt " ;
454486
455487 return $ this ->cacheOrFetchResult ($ url );
456488 }
@@ -516,11 +548,9 @@ public function getRawWeatherHistory($query, \DateTime $start, $endOrCount = 1,
516548 } else {
517549 throw new \InvalidArgumentException ('$endOrCount must be either a \DateTime or a positive integer. ' );
518550 }
519- $ queryUrl .= "&type= $ type&units= $ units&lang= $ lang " ;
551+ $ queryUrl .= "&type= $ type&units= $ units&lang= $ lang&APPID= " ;
520552
521- if (!empty ($ appid )) {
522- $ queryUrl .= "&APPID= $ appid " ;
523- }
553+ $ queryUrl .= empty ($ appid ) ? $ this ->apiKey : $ appid ;
524554
525555 return $ this ->cacheOrFetchResult ($ queryUrl );
526556 }
@@ -572,10 +602,8 @@ private function buildUrl($query, $units, $lang, $appid, $mode, $url)
572602 {
573603 $ queryUrl = $ this ->buildQueryUrlParameter ($ query );
574604
575- $ url = $ url . "$ queryUrl&units= $ units&lang= $ lang&mode= $ mode " ;
576- if (!empty ($ appid )) {
577- $ url .= "&APPID= $ appid " ;
578- }
605+ $ url = $ url . "$ queryUrl&units= $ units&lang= $ lang&mode= $ mode&APPID= " ;
606+ $ url .= empty ($ appid ) ? $ this ->apiKey : $ appid ;
579607
580608 return $ url ;
581609 }
0 commit comments