Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2014
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.97 KB | None | 0 0
  1. <?php
  2. class weather {
  3. private $_location;
  4. private $_url = 'http://www.google.com/ig/api?weather=';
  5. private $_isParsed = false;
  6. private $_wData;
  7.  
  8. public $lastError;
  9.  
  10. public function __construct( $location) {
  11. // Set location
  12. $this->_location = $location;
  13.  
  14. // urlencode doesn't seem to work so manually add the + for whitespace
  15. $this->_url = preg_replace('/s{1}/', '+',$this->_url .= $location);
  16. $this->parse_xml($this->get_xml());
  17. }
  18.  
  19. public function get_temp($type = "f") {
  20. if (!$this->_isParsed)
  21. return false;
  22.  
  23. // User specificed celsius, return celsius
  24. if ($type == "c")
  25. return $this->_wData['current']['temp_c'];
  26.  
  27. // return fahrenheit
  28. return $this->_wData['current']['temp_f'];
  29. }
  30.  
  31. public function get_condition() {
  32. if (!$this->_isParsed)
  33. return false;
  34. // provide current conditions only
  35. return $this->_wData['current']['condition'];
  36. }
  37.  
  38. public function get_forecast_for_day($day) {
  39. if (!$this->_isParsed)
  40. return false;
  41. return $this->_wData['forecast'][$day];
  42. }
  43. public function get_forecast_assoc() {
  44. if (!$this->_isParsed)
  45. return false;
  46. return $this->_wData['forecast'];
  47. }
  48.  
  49. public function get_cond_assoc() {
  50. if (!$this->_isParsed)
  51. return false;
  52. return $this->_wData['current'];
  53. }
  54.  
  55. public function dump_wData() {
  56. if (!$this->_isParsed)
  57. return false;
  58. return $this->_wData;
  59. }
  60.  
  61. public static function to_celsius($f) {
  62. // Convert Fahrenheit to Celsius.
  63. // I figured this would be quicker than trying to parse the XML.
  64. return floor(((int)$f - 32) * (5 / 9));
  65. }
  66.  
  67. private function get_xml() {
  68. // Download raw XML to be parsed.
  69. $ch = curl_init($this->_url);
  70.  
  71. // I don't know why I altered the useragent. It must have been for a good reason. Oh well.
  72. curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; rv:5.0.1) Gecko/20100101 Firefox/5.0.1');
  73. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  74. $rawXML = curl_exec($ch);
  75.  
  76. if (!$rawXML)
  77. return false;
  78. curl_close($ch);
  79. return $rawXML;
  80. }
  81.  
  82. private function parse_xml($xData) {
  83. libxml_use_internal_errors(true);
  84. try {
  85. $weather = new SimpleXMLElement($xData);
  86. } catch (Exception $err) {
  87. // Set $lastError to getMessage()
  88. $this->lastError = $err->getMessage();
  89. return false;
  90. }
  91. // Select the current_conditions node ($cNode)
  92. $cNode = $weather->weather[0]->current_conditions;
  93.  
  94. // ========= Set up our current conditions array ====================
  95.  
  96. // Tempreature - temp_f Fahrenheit, temp_c celsius - set as floats.
  97. $this->_wData['current']['temp_f'] = (float)$cNode->temp_f->attributes()->data;
  98. $this->_wData['current']['temp_c'] = weather::to_celsius($this->_wData['current']['temp_f']);
  99.  
  100. // Condition
  101. $this->_wData['current']['condition'] = (string)$cNode->condition->attributes()->data;
  102.  
  103. // Condition Icon - icon url is not absolute, append google.com
  104. $this->_wData['current']['icon'] = (string)"http://www.google.com" . $cNode->icon->attributes()->data;
  105.  
  106. // Wind Condition
  107. $this->_wData['current']['wind'] = (string)$cNode->wind_condition->attributes()->data;
  108.  
  109. // ============= Set up our forecast array =============
  110. $fNode = $weather->weather[0]->forecast_conditions;
  111.  
  112. // Iterate through each day of the week and create an assoc array.
  113. foreach ($fNode as $forecast) {
  114. // Get the day.
  115. $day = (string)$forecast->day_of_week->attributes()->data;
  116.  
  117. // Insert an array of info for that day
  118. $this->_wData['forecast'][$day] = array (
  119. "high" => (float)$forecast->high->attributes()->data,
  120. "low" => (float)$forecast->low->attributes()->data,
  121. "icon" => (string)"http://www.google.com" . $forecast->icon->attributes()->data,
  122. "condition" => (string)$forecast->condition->attributes()->data
  123. );
  124. } //foreach ($fNode as $forecast)
  125. // Let the class know wData is ready for use.
  126. $this->_isParsed = true;
  127. } //private function parse_xml($xData)
  128.  
  129. }
  130. ?>
  131.  
  132. <?php
  133. class GoogleWeather{
  134. protected $api = 'http://www.google.com/ig/api?weather=';
  135. protected $xml;
  136. protected $response;
  137. protected $location;
  138.  
  139. public function __construct( $location) {
  140. // Set location
  141. $this->location = $location;
  142. }
  143.  
  144. public function setResponse($response)
  145. {
  146. $this->response = $response;
  147. }
  148.  
  149. public function getResponse()
  150. {
  151. //if the xml hasn't been fetched, then fetch it
  152. if(empty($this->response)){
  153. $this->setResponse($this->fetchData());
  154. }
  155. return $this->response;
  156. }
  157.  
  158. //was get_xml renamed to avoid confusion
  159. public function fetchData()
  160. {
  161. // Download raw XML to be parsed.
  162. $ch = curl_init($this->api . urlencode($this->location));
  163.  
  164. // I don't know why I altered the useragent. It must have been for a good reason. Oh well.
  165. curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; rv:5.0.1) Gecko/20100101 Firefox/5.0.1');
  166. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  167. $rawXML = curl_exec($ch);
  168.  
  169. if (!$rawXML){
  170. //check the curl error for a better message
  171. throw new Exception('could not fetch data');
  172. }
  173. curl_close($ch);
  174. return $rawXML;
  175. }
  176.  
  177. public function getXml()
  178. {
  179. if(empty($this->xml)){
  180. try{
  181. $this->xml = new SimpleXMLElement($this->getResponse());
  182. } catch (Exception $e) {
  183. //there's no real recovery here, except maybe to retry fetching the data
  184. throw new Exception('bad response from from API');
  185. }
  186.  
  187. //check for 'problem_cause' element
  188. if(isset($this->xml->weather->problem_cause)){
  189. throw new Exception('API responded with error');
  190. }
  191. }
  192. return $this->xml;
  193. }
  194.  
  195. public function getCondition()
  196. {
  197. //make sure there's data
  198. if(!isset($this->getXml()->weather->current_conditions)){
  199. //you could throw an exception and assume any code using this is
  200. //calling it in a try block
  201. throw new Exception('could not find conditions');
  202. }
  203.  
  204. return $this->getXml()->weather->current_conditions;
  205. }
  206.  
  207. public function getTemp($type = 'f')
  208. {
  209. //validate type
  210. if(!in_array($type, array('f','c'))){
  211. throw Exception('invalid temp type: ' . $type);
  212. }
  213.  
  214. $element = 'temp_' . $type;
  215. //make sure there's data
  216. if(!isset($this->getCondition()->{$element}['data'])){
  217. throw new Exception('could not find temp');
  218. }
  219.  
  220. //cast as float and return
  221. return (float) $this->getCondition()->{$element}['data'];
  222. }
  223. }
  224.  
  225. public function getTemp($type = 'f')
  226. {
  227. static $temp = array();
  228.  
  229. //validate type
  230. if(!in_array($type, array('f','c'))){
  231. throw Exception('invalid temp type: ' . $type);
  232. }
  233.  
  234. if(isset($temp[$type])){
  235. return $temp[$type];
  236. }
  237.  
  238. $element = 'temp_' . $type;
  239. //make sure there's data
  240. if(!isset($this->getCondition()->{$element}['data'])){
  241. throw new Exception('could not find temp');
  242. }
  243.  
  244. //cast as float and return
  245. $temp[$type] = $this->getCondition()->{$element}['data'];
  246. return $temp[$type];
  247. }
  248.  
  249. <?php
  250. class weather {
  251. private $_location;
  252. private $_url = 'http://www.google.com/ig/api?weather=';
  253. private $_isParsed = false;
  254. private $_wData;
  255.  
  256. <?php
  257. class weather {
  258. private $_location;
  259. private $_url = 'http://www.google.com/ig/api?weather=';
  260. private $_isParsed = false;
  261. private $_wData;
  262.  
  263. public function get_cond_assoc() {
  264. if (!$this->_isParsed)
  265. return false;
  266. return $this->_wData['current'];
  267. }
  268.  
  269. public function get_cond_assoc() {
  270. if (!$this->_isParsed)
  271. {
  272. return false;
  273. }
  274. return $this->_wData['current'];
  275. }
  276.  
  277. // Condition Icon - icon url is not absolute, append google.com
  278. $this->_wData['current']['icon'] = (string)"http://www.google.com" .
  279. $cNode->icon->attributes()->data;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement