WS avec fichier WSDL

  • Client php client.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://localhost/LPRO/2016/ex3/Hello.wsdl', array('trace' => 1));
 
   $parm = 'LPRO';
    // Appel de la méthode hellotest du service web
 
   try{
    $oReturn =  $client -> hellotest($parm);
	} 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);
 
?>
  • Serveur PHP : serveur.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 Jour($parm) {
    	$aJour[1] = 'Lundi';
		$aJour[2] = 'Mardi';
		$aJour[3] = 'Mercredi';
		$aJour[4] = 'Jeudi';
		$aJour[5] = 'Vendredi';
		$aJour[6] = 'Samedi';
		$aJour[0] = 'Dimanche';
		return $aJour[date("w",strtotime($parm))];
    }
    function hellotest($parm) {
      return "Hello ".$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>';
}
 
 
?>
  • WSDL (généré avec eclipse) : Hello.wsdl
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://www.example.org/Hello/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="Hello" targetNamespace="http://www.example.org/Hello/">
  <wsdl:types>
    <xsd:schema targetNamespace="http://www.example.org/Hello/">
      <xsd:element name="hello">
        <xsd:complexType>
          <xsd:sequence>
            <xsd:element name="in" type="xsd:string"/>
          </xsd:sequence>
        </xsd:complexType>
      </xsd:element>
      <xsd:element name="helloResponse">
        <xsd:complexType>
          <xsd:sequence>
            <xsd:element name="out" type="xsd:string"/>
          </xsd:sequence>
        </xsd:complexType>
      </xsd:element>
    </xsd:schema>
  </wsdl:types>
  <wsdl:message name="hellotestRequest">
    <wsdl:part name="parameters" type="xsd:string"/>
  </wsdl:message>
  <wsdl:message name="hellotestResponse">
    <wsdl:part name="parameters" type="xsd:string"/>
  </wsdl:message>
  <wsdl:portType name="Hello">
    <wsdl:operation name="hellotest">
      <wsdl:input message="tns:hellotestRequest"/>
      <wsdl:output message="tns:hellotestResponse"/>
    </wsdl:operation>
  </wsdl:portType>
  <wsdl:binding name="HelloSOAP" type="tns:Hello">
    <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
    <wsdl:operation name="hellotest">
      <soap:operation soapAction="http://www.example.org/Hello/hellotest"/>
      <wsdl:input>
        <soap:body use="literal"/>
      </wsdl:input>
      <wsdl:output>
        <soap:body use="literal"/>
      </wsdl:output>
    </wsdl:operation>
  </wsdl:binding>
  <wsdl:service name="Hello">
    <wsdl:port binding="tns:HelloSOAP" name="HelloSOAP">
      <soap:address location="http://localhost/LPRO/2016/ex3/serveur.php"/>
    </wsdl:port>
  </wsdl:service>
</wsdl:definitions>