Ver Mensaje Individual
  #1 (permalink)  
Antiguo 30/11/2010, 14:37
Mxlive
 
Fecha de Ingreso: marzo-2008
Mensajes: 19
Antigüedad: 16 años, 7 meses
Puntos: 0
Sonrisa Ayuda Php5 e interpretación xml

Hola, tengo un script de prediccion meteorologica que utiliza la API google weather, y para geolocalizar al usuario otra api basada en la base de datos de maxmind.

El caso es que el script es compatible con php4 por que utiliza "simplexml.class" pero, yo se que desde php5 ya no es necesaria esa class por que php5 tiene soporte nativo para xml.

Entonces, me gustaria a ser posible si alguien podria introducir los cambios necesarios para que el script funcione sin necesidad de esta class. (no se programar php...)

Aqui esta el codigo:
Parte visual para el usuario:
Código PHP:
<?php 
    
require_once 'weather/includes/weather.class.php';
    
$weather = new Weather();
    
$informations $weather->getInformations();
    
$current $weather->getCurrent();
    
$forecast $weather->getForecast();
?>
<?php 
if(is_array($forecast)&&!empty($forecast)) { ?>
<div class="current">
    <p>Today</p>
    <h1><?php echo $current['temp_'.$weather->unitofmeasure];?>&deg;<?php echo strtoupper($weather->unitofmeasure);?></h1>
    <p><?php echo $informations['city'];?></p>
</div>
<?php foreach ($forecast as $k => $v) {?>
<div class="forecast">
    <p><?php echo $v['day_of_week'];?></p>
    <span style="background: url('images/weather/<?php echo $v['local_icons'];?>.png') no-repeat;"></span>
    <p><?php echo $v['high'];?>&deg;<?php echo strtoupper($weather->unitofmeasure);?> | <?php echo $v['low'];?>&deg;<?php echo strtoupper($weather->unitofmeasure);?> </p>
</div>     
<?php ?>
<?php 
} else { ?>
<div class="error">
    <h1>Weather is temporarily unavailable!</h1>
</div>
<?php ?>
Parte interna:
Código PHP:

require_once 'simplexml.class.php';

class 
Weather{
 
/*----------------- Options ----------------*/
    //Fahrenheit (f) Celsius (c)
    
var $unitofmeasure 'c';
    
//URLS
    
var $url 'http://www.google.com';
    var 
$url_params '/ig/api?weather=';
    var 
$url_lang '&hl=es';
    var 
$url_location 'http://api.ipinfodb.com/v2/ip_query.php?key=560d860423dfac90825d0abb19dcfe60f21ba9fc4e372c3677293bd5d3b5b072&ip=';
    
//Custom Icons
    
var $local_icons '1';
/*----------------- Options ----------------*/
    
    
var $location "";
     var 
$wArray "";
    var 
$api_url "";
    var 
$counter "";
        
        function 
Weather($location="") {
            
            
$this->api_url $this->url.$this->url_params;
            
//We may force a place
            
if ($location!=""){
                
$this->location $location;
            } else {
                
$this->locateip();
            }            
            
$this->makeArray();
        }
        
        function 
getInformations(){
            
$array $this->wArray;
            
            unset(
$array['forecast_conditions']);
            unset(
$array['current_conditions']);
            
            return 
$array['forecast_information'];
        }
        function 
getCurrent(){
            
$array $this->wArray;
            
            unset(
$array['forecast_conditions']);
            unset(
$array['forecast_information']);
            
            return 
$array['current_conditions'];
        }
        
        function 
getForecast(){
            
$array $this->wArray;
            
            unset(
$array['current_conditions']);
            unset(
$array['forecast_information']);
            
            return 
$array['forecast_conditions'];
        }
            
        function 
makeArray(){
            
// build the url
            
$url $this->api_url $this->urlEncode($this->location);
                        
            
$xml = new simplexml();
            
$result $xml -> xml_load_file($url,'array');
                        
            
$return = array();
            
$main_array = array();
            
//First pass (we strip @attributes)
            
$main_array $result['weather'];
                
            
//Forecast Information
            
$forecast_information = array();
            
$forecast_information $main_array['forecast_information'];
            
$return['forecast_information']['city'] = $forecast_information['city']['@attributes']['data'];
            
$return['forecast_information']['zip'] = $forecast_information['city']['@attributes']['data'];
            
$return['forecast_information']['date'] = $forecast_information['city']['@attributes']['data'];
            
$return['forecast_information']['date_time'] = $forecast_information['city']['@attributes']['data'];
                
            
//Current Conditions
            
$current_conditions = array();
            
$current_conditions $main_array['current_conditions'];
            
$return['current_conditions']['condition'] = $current_conditions['condition']['@attributes']['data'];
            
$return['current_conditions']['temp_f'] = $current_conditions['temp_f']['@attributes']['data'];
            
$return['current_conditions']['temp_c'] = $current_conditions['temp_c']['@attributes']['data'];
            
$return['current_conditions']['humidity'] = $current_conditions['humidity']['@attributes']['data'];
            
$return['current_conditions']['icon'] = $current_conditions['icon']['@attributes']['data'];
            
$return['current_conditions']['wind'] = $current_conditions['wind_condition']['@attributes']['data'];
            
//Custom icons
            
if ($this->local_icons == '1') {
                
$local $this->cleanIcon($current_conditions['icon']['@attributes']['data']);
                 
$return['current_conditions']['local_icons'] = $local;
            }
            
            
//Forecast Conditions
            
$forecast_conditions = array();
             
$forecast_conditions $main_array['forecast_conditions'];

            for (
$i 0$i count($forecast_conditions); $i++){
                
$return['forecast_conditions'][$i]['day_of_week'] = $forecast_conditions[$i]['day_of_week']['@attributes']['data'];
                
//If the user need celsius we need to convert.
                
if ($this->unitofmeasure=='c'){
                    
$return['forecast_conditions'][$i]['low'] = $this->fromFtoC($forecast_conditions[$i]['low']['@attributes']['data']);
                    
$return['forecast_conditions'][$i]['high'] = $this->fromFtoC($forecast_conditions[$i]['high']['@attributes']['data']);
                } else {
                    
$return['forecast_conditions'][$i]['low'] = $forecast_conditions[$i]['low']['@attributes']['data'];
                    
$return['forecast_conditions'][$i]['high'] = $forecast_conditions[$i]['high']['@attributes']['data'];
                }
                
$return['forecast_conditions'][$i]['icon'] = $forecast_conditions[$i]['icon']['@attributes']['data'];
                
$return['forecast_conditions'][$i]['condition'] = $forecast_conditions[$i]['condition']['@attributes']['data'];
                
//
                
if ($this->local_icons == '1') {
                    
$local_icons $this->cleanIcon($forecast_conditions[$i]['icon']['@attributes']['data']);
                    
$return['forecast_conditions'][$i]['local_icons'] = $local_icons;
                }
            }
             
$this->counter $i;
            
$this->wArray $return;
     
        }
        
//
        
function locateIp() {
            
            
$ip $_SERVER["REMOTE_ADDR"];
            
            
$xml = new simplexml();
            
            
$url $this->url_location.$ip.'&output=xml';
            
$result $xml -> xml_load_file($url,'array');
            
            if ( 
$result['CountryName'] && $result['RegionName'] && $result['City'] ) {
                
$this->location $result['City'].','.$result['RegionName'].','.$result['CountryName'];
            }
            return 
true;
        }
         
//
        
function fromFtoC($degrees){
            
$degrees = ($degrees-32);
            
$degrees = ($degrees*5);
            
$degrees = ($degrees/9);
            return 
round($degrees);
        }
        
//
        
function urlEncode($string){
            
            
$htmlentites htmlentities($stringENT_QUOTES'UTF-8');
            
$firstPass preg_replace('~&([a-z]{1,2})(acute|cedil|circ|grave|lig|orn|ring|slash|th|tilde|uml);~i''$1'$htmlentites);
            
$secondPass preg_replace('~[^0-9a-z,]+~i''+',$firstPass);
            
$return strtolower(trim($secondPass' '));
            unset(
$htmlentites); unset($firstPass); unset($secondPass);
            return 
$return;
        }
        
//
        
function cleanIcon($string){
            
$string substr($string,19);
            
$stringLen strlen($string);
            
//We subtract 4 (.gif)
            
$pos $stringLen 4;
            return 
trim(substr($string0$pos));
        }
    }
?> 
Muchísimas gracias de antemano a quien pueda hacerlo.