Usuario:ManuelRomero/ProgramacionWeb/WS/soap/practica
De WikiEducator
Ejemplo de usar un servicio web
servido web : http://www.webservicex.net/New/Home/ServiceDetail/56
|
Generación de la clase Temperatura a partir del wsdl |
---|
<?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; } } ?> |
Uso del servicio |
---|
<!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> |