Page 1 sur 1

API google weather ... casse tête

Posté : 25 août 2012, 22:23
par gigipop
Bonjour,

Je reviens vers vous car je butte avec l'API google weather.
Elle fonctionne par intermitence, un coup ça marche, un coup ça marche... et très peu de doc sur le net...

Voici la class que j'utilise :
<?php

if(isset($_GET['source'])) {
    highlight_file(__FILE__);
    die;
}

/***************************************************************
 *
 * 09/05/2009
 *
 * Copyright notice
 *
 * (c) 2009 Yohann CERDAN <[email protected]>
 * 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);

			echo $xml;
			
			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 '<pre>'; print_r($gweather->getCity()); echo '</pre>';
	echo '<pre>'; print_r($gweather->getCurrent()); echo '</pre>';
	echo '<pre>'; print_r($gweather->getForecast()); echo '</pre>';
}
*/
?>
Et les messages d'erreurs récurent ( 3 fois sur 5...)
Auriez vous une solution ?
Warning: simplexml_load_string() [function.simplexml-load-string]: Entity: line 1: parser error : Start tag expected, '<' not found in /Applications/MAMP/htdocs/telex_online/class/GoogleWeather.class.php on line 68

Warning: simplexml_load_string() [function.simplexml-load-string]: Unsupported API in /Applications/MAMP/htdocs/telex_online/class/GoogleWeather.class.php on line 68

Warning: simplexml_load_string() [function.simplexml-load-string]: ^ in /Applications/MAMP/htdocs/telex_online/class/GoogleWeather.class.php on line 68

Warning: simplexml_load_string() [function.simplexml-load-string]: Entity: line 1: parser error : Start tag expected, '<' not found in /Applications/MAMP/htdocs/telex_online/class/GoogleWeather.class.php on line 74

Warning: simplexml_load_string() [function.simplexml-load-string]: Unsupported API in /Applications/MAMP/htdocs/telex_online/class/GoogleWeather.class.php on line 74

Warning: simplexml_load_string() [function.simplexml-load-string]: ^ in /Applications/MAMP/htdocs/telex_online/class/GoogleWeather.class.php on line 74

Notice: Trying to get property of non-object in /Applications/MAMP/htdocs/telex_online/class/GoogleWeather.class.php on line 76

Notice: Trying to get property of non-object in /Applications/MAMP/htdocs/telex_online/class/GoogleWeather.class.php on line 76

Notice: Trying to get property of non-object in /Applications/MAMP/htdocs/telex_online/class/GoogleWeather.class.php on line 76

Fatal error: Call to a member function attributes() on a non-object in /Applications/MAMP/htdocs/telex_online/class/GoogleWeather.class.php on line 76
Merci d'avance,

Re: API google weather ... casse tête

Posté : 27 août 2012, 10:41
par moogli
salut,

Perso j'ai trouvé un peu de documentation, dont celle de l'auteur de la classe que tu utilise :)

après un simple test visible ici, la réponse est assez sympa : Unsupported API :mrgreen:

je ne suis pas certain que cela soit encore possible.

regarde du coté de la même chose en javascript , comme par exemple
https://developers.google.com/maps/docu ... atherLayerPeut être que google ne propose plus ce service ? ou d'une autre manière (JS) peut être que l'auteur de la classe en sais plus ?


@+