-
Notifications
You must be signed in to change notification settings - Fork 0
/
WundergroundWeather.php
81 lines (68 loc) · 1.88 KB
/
WundergroundWeather.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?php
/**
* @access Public
* @version 1.0
* @Author Utkarsh Singh
* @Details This class is responsible to wunderground Weathers API Call
**/
Class WundergroundWeather{
private $app_key;
private $apiUrl;
private $weatherArray;
private $features;
/**
* Constructor will set protected variables of class
*/
public function __construct($viewObj=''){
$this->weatherArray = array();
$this->app_key = '<use your API key here>';
$this->apiUrl = 'http://api.wunderground.com/';
ini_set('memory_limit', '2G');
}
/*@params
*
*@function to reset the weatherArray array
*/
public function __resetWeatherArray(){
$this->weatherArray = array();
}
/*@params array
*@return array
*@Request for get weather details
*/
public function fetchWeather($features,$zipcode,$format){
set_time_limit(0);
try{
$url = $this->apiUrl;
$app_key = $this->app_key;
$qStr = "";
$qStr.='api/'.$app_key;
foreach($features as $feature){
$qStr.='/'.$feature;
}
$qStr.="/q/".$zipcode;
$qStr.=".".$format;
$url .= $qStr;
$curl_handle = curl_init();
curl_setopt($curl_handle, CURLOPT_URL, $url);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
$searchResponse = curl_exec($curl_handle);
curl_close($curl_handle);
$searchResponse = json_decode($searchResponse);
//convert object to array
$searchResponse = $this->objectToArray($searchResponse);
unset($searchResponse['response']);
$this->weatherArray = $searchResponse;
return $this->weatherArray;
}catch(Exception $e){
return $e->getMessage();
}
}
/* @params object
* @return array.
* @convert object into array.
*/
function objectToArray($object){
return @json_decode(@json_encode($object),1);
}
}