Accueil/modules/worldweatheronline/includes/WorldweatheronlineApi.php

160 lines
5.3 KiB
PHP

<?php
class WorldweatheronlineApi
{
/** City code input **/
private $city_code = '613135'; // Montsoult
/** 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;
/** API KEY **/
const KEY = '60dc184fe4be1b80bbaa8cd1103b0ab8e45a25f0';
/**
* Class constructor
* @param string $city_code is the label of the city
* @param string $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;
$this->url = 'http://api.worldweatheronline.com/free/v1/weather.ashx?q=' . $this->city_code . '&format=xml&num_of_days=3&key=' . self::KEY;
$getContentCode = $this->getContent($this->url);
if ($getContentCode == 200) {
$content = utf8_encode($this->response);
$xml = simplexml_load_string($content, 'SimpleXMLElement', LIBXML_NOCDATA);
if (!isset($xml->error)) {
//if(!isset($xml->weather->problem_cause)) {
//$this->city = (string)$xml->weather->forecast_information->city->attributes()->data;
$this->city = (string)$xml->request->query;
$this->current_conditions['condition'] = (string)$xml->current_condition->weatherDesc;
$this->current_conditions['temp_f'] = (string)$xml->current_condition->temp_F;
$this->current_conditions['temp_c'] = (string)$xml->current_condition->temp_C;
//$this->current_conditions['temp_c'] = (string)$xml->weather->current_conditions->temp_c->attributes()->data;
$this->current_conditions['humidity'] = (string)$xml->current_condition->humidity;
//$this->current_conditions['humidity'] = (string)$xml->weather->current_conditions->humidity->attributes()->data;
$this->current_conditions['icon'] = (string)$xml->current_condition->weatherIconUrl;
//$this->current_conditions['icon'] = $this->prefix_images.(string)$xml->weather->current_conditions->icon->attributes()->data;
$this->current_conditions['wind_condition'] = (string)$xml->current_condition->windspeedKmph;
//$this->current_conditions['wind_condition'] = (string)$xml->weather->current_conditions->wind_condition->attributes()->data;
foreach ($xml->weather as $forecast_conditions_value) {
$forecast_conditions_temp = array();
$forecast_conditions_temp['day_of_week'] = (string)$forecast_conditions_value->date;
$forecast_conditions_temp['low'] = (string)$forecast_conditions_value->tempMinC;
$forecast_conditions_temp['high'] = (string)$forecast_conditions_value->tempMaxC;
$forecast_conditions_temp['icon'] = (string)$forecast_conditions_value->weatherIconUrl;
$forecast_conditions_temp['condition'] = (string)$forecast_conditions_value->weatherDesc;
$this->forecast_conditions[] = $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
*/
protected function getContent($url)
{
if (!extension_loaded('curl')) {
throw new Exception('curl extension is not available');
}
$useragent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1";
$curl = curl_init();
curl_setopt($curl, CURLOPT_TIMEOUT, 10);
curl_setopt($curl, CURLOPT_USERAGENT, $useragent);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10);
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
*/
public function getCity()
{
return $this->city;
}
/**
* Get the current weather
*/
public function getCurrent()
{
return $this->current_conditions;
}
/**
* Get the forecast weather
*/
public function getForecast()
{
return $this->forecast_conditions;
}
/**
* Get the icon
*/
public function getIcon()
{
return $this->current_conditions['icon'];
}
/**
* If teh city was found
*/
public function isFound()
{
return $this->is_found;
}
}