Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
lpro:ws [2017/12/11 09:33]
jbaudry
lpro:ws [2017/12/18 09:58] (current)
jbaudry [Liens webservices]
Line 20: Line 20:
  
 {{:​lpro:​05-rest.pdf| Introduction REST}} {{:​lpro:​05-rest.pdf| Introduction REST}}
 +
 +==== Liens webservices ====
 +  * http://​litis.univ-lehavre.fr/​wiki/​lpro/​ws/​rest
 +  * Livres
 +    * https://​www.googleapis.com/​books/​v1/​volumes?​q=isbn:​0747532699
 +  * Adresse
 +    * https://​maps.googleapis.com/​maps/​api/​geocode/​json?​address=universite%20du%20havre,​%20france&​sensor=false
 +  * Iceland API gouv
 +    * http://​docs.apis.is/​
 +  * Photos
 +    * https://​www.flickr.com/​services/​feeds/​docs/​photos_public/​
 +  * Pays
 +    * https://​restcountries.eu
 +  * Pays 2
 +    * https://​geo.api.gouv.fr/​communes?​codePostal=76600&​fields=nom,​code,​codesPostaux,​codeDepartement,​codeRegion,​population&​format=json&​geometry=centre'​
 +  * Adresses
 +    * https://​adresse.data.gouv.fr/​api/​
 +
  
   * http://​blog.programmableweb.com/​2012/​01/​11/​12-json-weather-apis/​   * http://​blog.programmableweb.com/​2012/​01/​11/​12-json-weather-apis/​
   * [[lpro:​ws:​rest_php|Exemple en php]]   * [[lpro:​ws:​rest_php|Exemple en php]]
   * WSDL : http://​www.weather.gov/​forecasts/​xml/​DWMLgen/​wsdl/​ndfdXML.wsdl   * WSDL : http://​www.weather.gov/​forecasts/​xml/​DWMLgen/​wsdl/​ndfdXML.wsdl
-  * WSDL : +  * WSDL La poste {{ :​lpro:​spec_ws_suivi_en.pdf |}}
  
  
Line 75: Line 93:
  
   * Python   * Python
-<​code ​php>+ 
 +<​code ​python>
 from SOAPpy import WSDL from SOAPpy import WSDL
 import zeep import zeep
  
-client = Client('​http://​www.webservicex.net/​ConvertSpeed.asmx?​WSDL'​)+client = zeep.Client('​http://​www.webservicex.net/​ConvertSpeed.asmx?​WSDL'​)
 result = client.service.ConvertSpeed( result = client.service.ConvertSpeed(
    102, '​kilometersPerhour',​ '​milesPerhour'​)    102, '​kilometersPerhour',​ '​milesPerhour'​)
Line 87: Line 106:
   * Conversion de température fahrenheit -> Celsius   * Conversion de température fahrenheit -> Celsius
   * Réalisez le même exercice que précédemment en utilisant la fonction inverse pour convertir une température de fahrenheit -> Celsius.   * Réalisez le même exercice que précédemment en utilisant la fonction inverse pour convertir une température de fahrenheit -> Celsius.
-  * Utilisez Eclipse pour vous connecter à ce webservice+  * Utilisez Eclipse pour vous connecter à ce webservice ​: http://​www.webservicex.net/​ConvertSpeed.asmx?​WSDL
 <code java> <code java>
 public static void main(String[] args) { public static void main(String[] args) {
Line 101: Line 120:
  }  }
  }  }
 +</​code>​
 +
 +<code java>
 +public class TestSpeed {
 +
 + public static void main(String[] args) {
 + // TODO Auto-generated method stub
 + ConvertSpeedsSoapProxy c = new ConvertSpeedsSoapProxy();​
 +
 + SpeedUnit s1 = new SpeedUnit(SpeedUnit._kilometersPerhour);​
 + SpeedUnit s2 = new SpeedUnit(SpeedUnit._milesPerhour);​
 + try {
 + System.out.println(c.convertSpeed(102,​ s1, s2));
 + } catch (RemoteException e) {
 + // TODO Auto-generated catch block
 + e.printStackTrace();​
 + }
 + }
 +}
 </​code>​ </​code>​
 ===== Exercice 3 : Consommer des WebServices en PHP/Java ===== ===== Exercice 3 : Consommer des WebServices en PHP/Java =====
Line 167: Line 205:
 ?> ?>
 </​code>​ </​code>​
 +
 +  * WSDL : Client
 +
 +<code php>
 +<?php
 +// Pour supprimer le cache du web-service
 +ini_set('​soap.wsdl_cache_enabled',​ 0);
 + 
 +// Nouveau Client SOAP
 +try {
 +    // Nouvelle instance de la classe soapClient
 +   ​$client = new SoapClient('​http://​127.0.0.1/​lpro/​ws1/​Hello.wsdl',​ array('​trace'​ => 1));
 + 
 +   $parm = '​LPRO';​
 +    // Appel de la méthode hellotest du service web
 + 
 +   try{
 +    $oReturn =  $client->​bonjour("​LPRO"​);​
 +
 + } catch (Exception $e) {
 +    echo '​erreur1'​.$e;​
 +
 + catch (SoapFault $fault) {
 + echo "il y a une erreur";​
 +    trigger_error("​SOAP Fault: (faultcode: {$fault->​faultcode},​ faultstring:​ {$fault->​faultstring})",​ E_USER_ERROR);​
 + }
 +        // affiche le résultat
 + echo "​Resultat : "​.$oReturn."<​br/>"​ ;
 +} catch (SoapFault $fault) {
 +    echo '​erreur : '​.$fault;​
 +}
 + 
 +// Affichage des requetes et reponses SOAP (pour debug)
 + 
 + echo '<​br />​Requete SOAP : '​.htmlspecialchars($client->​__getLastRequest()).'<​br />';​
 + 
 + echo '<​br />​Reponse SOAP : '​.htmlspecialchars($client->​__getLastResponse()).'<​br />';​
 + 
 +  echo '<​br/>'​.var_dump($oReturn);​
 + 
 +?>
 +</​code>​
 +
 +  * WSDL : server
 +
 +<code php>
 +<?php
 +// Pour supprimer le cache du web-service
 +ini_set('​soap.wsdl_cache_enabled',​ 0);
 +// Pour définir le temp maximal d'​éxecution de notre web-service
 +ini_set('​default_socket_timeout',​ 180);
 + 
 +// Nom de notre web-service
 +class Server {
 +     // Le service QuelJour que l'on a définit dans notre format wsdl
 +
 + function bonjour($parm) {
 +    return "​bonjour "​.$parm ;
 +    }
 +}
 + 
 + 
 +// On tente d'​instancier la classe soapServer
 +// Si cela s'​avère impossible, on affiche une erreur
 +try {
 +    $server = new SoapServer('​Hello.wsdl', ​ array('​trace'​ => 1,'​encoding'​ =>​ '​ISO-8859-1'​));​
 +    // On définit la classe qui va gérer les requêtes SOAP (pour nous c'est la class Server)
 +    $server->​setclass('​Server'​);​
 +    $server->​setPersistence(SOAP_PERSISTENCE_REQUEST);​
 +} catch (Exception $e) {
 +    echo '​erreur'​.$e;​
 +}
 + 
 + 
 +/*
 +* Gestion des requêtes
 +*/
 + 
 +// Si l'​appel provient d'un Web-Service
 +if ($_SERVER['​REQUEST_METHOD'​] == '​POST'​) {
 + try {
 + $server -> handle();}
 + catch (Exception $e) {
 +    echo '​erreur'​.$e;​
 + }
 +}
 +// Facultatif seulement pour montrer les fonctions disponibles par le web-service
 +else {
 +    echo '<​strong>​This SOAP server can handle following functions : </​strong>';​
 +    echo '<​ul>';​
 +    foreach($server -> getFunctions() as $func) {
 +        echo '<​li>'​ , $func , '</​li>';​
 +    }
 +    echo '</​ul>';​
 +}
 + 
 + 
 +?>
 +</​code>​
 +
 ====== Exercice 5 : Manipuler le format JSON (open data Nantes) ====== ====== Exercice 5 : Manipuler le format JSON (open data Nantes) ======