From 14e63a784427d5f0b3ba0c3989acdc61dff0c2dd Mon Sep 17 00:00:00 2001 From: Chouchen Date: Wed, 15 Sep 2010 11:05:52 +0000 Subject: [PATCH] =?UTF-8?q?Installation=20du=20script=20GoogleMeteo.php=20?= =?UTF-8?q?en=20=C3=A9change=20de=20meteo.php=20pour=20choix=20de=20la=20v?= =?UTF-8?q?ille=20+=20lien=20vers=20enregistrement=20todoist?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- GoogleMeteo.php | 36 ++++++++ class/GoogleWeather.class.php | 168 ++++++++++++++++++++++++++++++++++ db/config.xml | 1 + index.php | 3 +- install-todoist.php | 1 + 5 files changed, 208 insertions(+), 1 deletion(-) create mode 100644 GoogleMeteo.php create mode 100644 class/GoogleWeather.class.php diff --git a/GoogleMeteo.php b/GoogleMeteo.php new file mode 100644 index 0000000..5e1adb3 --- /dev/null +++ b/GoogleMeteo.php @@ -0,0 +1,36 @@ +isFound()) { + $currentTemp = $gweather->getCurrent(); + $forecastTemp = $gweather->getForecast(); + } + $present_weather = 'Actuellement : '.$currentTemp['temp_c'].'°C - '.$currentTemp['condition']; + $i=0; + foreach($forecastTemp as $temp) { + ${'future_weather'.$i} = $temp['day_of_week'].' : '.$temp['low'].'°C | '.$temp['high'].'°C - '.$temp['condition']; + $i++; + } + + if(strpos(strtolower($present_weather),"soleil") !== false) {?> + + + + + + + + + + + * All rights reserved + * + * This script is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * This copyright notice MUST APPEAR in all copies of the script! + ***************************************************************/ + +class GoogleWeatherAPI { + /** City code input **/ + private $city_code = ''; + + /** City label get on the google webservice **/ + private $city = ''; + + /** Domain of the google website **/ + private $domain = 'www.google.com'; + + /** Prefix of the img link **/ + private $prefix_images = ''; + + /** Array with current weather **/ + private $current_conditions = array(); + + /** Array with forecast weather **/ + private $forecast_conditions = array(); + + /** If the city was found **/ + private $is_found = true; + + /** The HTML response send by the service **/ + private $response; + + /** + * Class constructor + * @param $city_code is the label of the city + * @param $lang the lang of the return weather labels + * @return ... + */ + + function __construct ($city_code,$lang='fr') { + $this->city_code = $city_code; + $this->prefix_images = 'http://'.$this->domain; + $this->url = 'http://'.$this->domain.'/ig/api?weather='.urlencode($this->city_code).'&hl='.$lang; + + $getContentCode = $this->getContent($this->url); + + if($getContentCode == 200) { + + $content = utf8_encode($this->response); + + $xml = simplexml_load_string($content); + + if(!isset($xml->weather->problem_cause)) { + + $xml = simplexml_load_string($content); + + $this->city = (string)$xml->weather->forecast_information->city->attributes()->data; + + $this->current_conditions['condition'] = (string)$xml->weather->current_conditions->condition->attributes()->data; + $this->current_conditions['temp_f'] = (string)$xml->weather->current_conditions->temp_f->attributes()->data; + $this->current_conditions['temp_c'] = (string)$xml->weather->current_conditions->temp_c->attributes()->data; + $this->current_conditions['humidity'] = (string)$xml->weather->current_conditions->humidity->attributes()->data; + $this->current_conditions['icon'] = $this->prefix_images.(string)$xml->weather->current_conditions->icon->attributes()->data; + $this->current_conditions['wind_condition'] = (string)$xml->weather->current_conditions->wind_condition->attributes()->data; + + foreach($xml->weather->forecast_conditions as $this->forecast_conditions_value) { + $this->forecast_conditions_temp = array(); + $this->forecast_conditions_temp['day_of_week'] = (string)$this->forecast_conditions_value->day_of_week->attributes()->data; + $this->forecast_conditions_temp['low'] = (string)$this->forecast_conditions_value->low->attributes()->data; + $this->forecast_conditions_temp['high'] = (string)$this->forecast_conditions_value->high->attributes()->data; + $this->forecast_conditions_temp['icon'] = $this->prefix_images.(string)$this->forecast_conditions_value->icon->attributes()->data; + $this->forecast_conditions_temp['condition'] = (string)$this->forecast_conditions_value->condition->attributes()->data; + $this->forecast_conditions []= $this->forecast_conditions_temp; + } + } else { + $this->is_found = false; + } + + } else { + trigger_error('Google results parse problem : http error '.$getContentCode,E_USER_WARNING); + return null; + } + } + + /** + * Get URL content using cURL. + * + * @param string $url the url + * + * @return string the html code + */ + + public function getContent($url) + { + if (!extension_loaded('curl')) { + throw new Exception('curl extension is not available'); + } + + $curl = curl_init(); + curl_setopt($curl, CURLOPT_TIMEOUT, 10); + curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 5); + curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE); + curl_setopt($curl, CURLOPT_URL, $url); + $this->response = curl_exec($curl); + $infos = curl_getinfo($curl); + curl_close ($curl); + return $infos['http_code']; + } + + /** + * Get the city + */ + + function getCity() { + return $this->city; + } + + /** + * Get the current weather + */ + + function getCurrent() { + return $this->current_conditions; + } + + /** + * Get the forecast weather + */ + + function getForecast() { + return $this->forecast_conditions; + } + + /** + * If teh city was found + */ + + function isFound() { + return $this->is_found; + } + +} +/* +$gweather = new GoogleWeatherAPI('nantes','fr'); // "en" also work +if($gweather->isFound()) { + echo '
'; print_r($gweather->getCity()); echo '
'; + echo '
'; print_r($gweather->getCurrent()); echo '
'; + echo '
'; print_r($gweather->getForecast()); echo '
'; +} +*/ +?> \ No newline at end of file diff --git a/db/config.xml b/db/config.xml index 7f03b04..66eb6be 100644 --- a/db/config.xml +++ b/db/config.xml @@ -7,6 +7,7 @@ true + Paris 488 658 diff --git a/index.php b/index.php index 4385d23..72eb410 100644 --- a/index.php +++ b/index.php @@ -22,7 +22,8 @@ if($config = simplexml_load_file('db/config.xml')){ visibility == "true"){ - include 'meteo.php'; + $city = $config_xml[1]->city; + include 'GoogleMeteo.php'; ?> token == '' || $xmla->token == null):?> Cliquez ici pour vous identifier
+ ou Cliquez ici pour vous créer un compte (ainsi qu'un projet ;))