Usuario:ManuelRomero/ProgramacionWeb/WS/soap/practica

De WikiEducator
Saltar a: navegación, buscar


Ejemplo de usar un servicio web



Icon activity.jpg
Mostrar la temperatura de un país
servido web : http://www.webservicex.net/New/Home/ServiceDetail/56 
  • Se trata de hacer una aplicación que use el servicio anterior
  • Se pide que generemos una clase para usar el servicio
  • Primero mostraremos todas las ciudades de un país establecido
  • Posteriormente al seleccionar una ciudad dará la información de la temperatura de ese país



<?php
 
class GetWeather {
 
    var $CityName; //string
    var $CountryName; //string
 
}
 
class GetWeatherResponse {
 
    var $GetWeatherResult; //string
 
}
 
class GetCitiesByCountry {
 
    var $CountryName; //string
 
}
 
class GetCitiesByCountryResponse {
 
    var $GetCitiesByCountryResult; //string
 
}
 
class Temparaturas {
 
    var $soapClient;
    private static $classmap = array('GetWeather' => 'GetWeather'
        , 'GetWeatherResponse' => 'GetWeatherResponse'
        , 'GetCitiesByCountry' => 'GetCitiesByCountry'
        , 'GetCitiesByCountryResponse' => 'GetCitiesByCountryResponse'
    );
 
    function __construct($url = 'http://www.webservicex.com/globalweather.asmx?WSDL') {
        $this->soapClient = new SoapClient($url, array("classmap" => self::$classmap, "trace" => true, "exceptions" => true));
    }
 
    function GetCitiesByCountry(GetCitiesByCountry $GetCitiesByCountry) {
 
        $GetCitiesByCountryResponse = $this->soapClient->GetCitiesByCountry($GetCitiesByCountry);
        return $GetCitiesByCountryResponse;
    }
 
    function GetWeather(GetWeather $GetWeather) {
 
        $GetWeatherResponse = $this->soapClient->GetWeather($GetWeather);
        return $GetWeatherResponse;
    }
 
}
?>
{{{3}}}
<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<?php
require ('Temperaturas.php');
 
function mostrar_temperatura($ciudad, $pais) {
    $cliente = new Temparaturas();
    $ubicacion = new GetWeather();
    $ubicacion->CityName = $ciudad;
    $ubicacion->CountryName = $pais;
    $temp = $cliente->GetWeather($ubicacion);
    echo "<h1>Valor de la temperatura</h1>";
    var_dump($temp);
}
 
function mostrar_ciudades($pais) {
    $cliente = new Temparaturas();
    $ciudad = new GetCitiesByCountry();
    $ciudad->CountryName = $pais;
    $ciudades = $cliente->GetCitiesByCountry($ciudad);
    $ciudades_array = new SimpleXMLElement($ciudades->GetCitiesByCountryResult);
    $msj = "<select name='ciudad' >";
    foreach ($ciudades_array->Table as $index => $pais)
        $msj .= "<option name=$pais->City>$pais->City</option>";
    $msj .= "</select >";
    return $msj;
}
 
switch ($_POST['enviar']) {
    case 'ciudades':
        $pais = $_POST['pais'];
        $msj = mostrar_ciudades($pais);
        break;
    case 'temperatura':
        $ciudad = $_POST['ciudad'];
        $pais = $_POST['pais'];
        mostrar_temperatura($ciudad, $pais);
        break;
}
?>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        <form action="index.php" method="POST">
            <input type="text" value="SPAIN" name='pais'>
            <?php echo "Selecciona ciudad $msj " ?>
            <input type="submit" value="ciudades" name ="enviar">
            <input type="submit" value="temperatura" name ="enviar">
        </form>
    </body>
</html>
{{{3}}}