query = $query; $this->url = $url; $this->_retrieveDATA(); $this->dom = new DomDocument(); $this->dom->loadXML($this->data); if ($this->_isResultEmpty()) return false; } /** * Queries GoogleWeather API. * uses PHP Curl extention. */ protected function _retrieveDATA() { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $this->url . $this->query); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $data = curl_exec($ch); $this->data = $this->_tidyXML($data); } /** * Cleans up malformed XML Data. * * @param string $xml XML Formatted string * @return string */ protected function _tidyXML($xml) { $options = array('input-xml' => true, 'output-xml' => true, 'clean' => true); $clean = tidy_parse_string($xml, $options, 'UTF8'); tidy_clean_repair($clean); return $clean; } /** * Checks if returned DATA has results. * * @return bool */ protected function _isResultEmpty() { $error = $this->dom->getElementsByTagName('problem_cause')->item(0); return $error; } /** * Gets the current weather condition. * * @return array */ public function getCurrentCondition() { $node = $this->dom->getElementsByTagName('current_conditions')->item(0); $current = array(); foreach ($node->childNodes as $c) { $current[$c->nodeName] = $c->getAttribute('data'); } return $current; } /** * Get weather conditions for all days. As of now * Google currently returns 4 days. * * @return array */ public function getAllDays() { $nodes = $this->dom->getElementsByTagName('forecast_conditions'); $i = 1; $weather = array(); foreach ($nodes as $node) { foreach ($node->childNodes as $c) { if ($c->hasAttributes()) $weather[$i][$c->nodeName] = $c->getAttribute('data'); } $i++; } return $weather; } } // set tabstop=4