[Web Service] SOAP serveur PHP client J2EE
Bonjour
Après de longue heures de recherche et de modification j'ai réussi a faire tourner un exemple SOAP "serveur PHP"+" client J2EE".
Voici le codes source pour ceux qui les intéresses.
crée un dossier sous wamp ou EasyPhP
www/soap/server/
crée le fichier
index.php
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| <?php
// On désactive la mise en cache du wsdl (pour le test)
ini_set('soap.wsdl_cache_enabled', 0);
// Inclusion de la classe contenant l'implémentation des fonctions du Service Web
include('WebServiceFirst.php');
// Tentative d'instanciation du serveur SOAP
try {
$server = new SoapServer('server.wsdl', array('trace' => 1, 'soap_version' => SOAP_1_1));
$server -> setclass('WebServiceFirst');
}
catch (Exception $e) {
//TODO Traitement en cas d'exception, pour l'instant on l'affiche tel quel...
echo $e;
exit();
}
// Appel du Service Web (requête POST uniquement autorisée)
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// Prise en charge de la requête
$server -> handle();
}
// Sinon, on affiche la liste des fonctions du serveur SOAP et un lien vers le WSDL
else {
echo 'Web Service First.<br />';
echo '<a href="server.wsdl">WSDL</a><br />';
echo 'Fonctions :';
echo '<ul>';
foreach($server -> getFunctions() as $func) {
echo '<li>' , $func , '</li>';
}
echo '</ul>';
}
?> |
crée le fichier
server.wsdl
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| <?xml version="1.0" encoding="UTF-8" ?>
<definitions name='ERIC'
targetNamespace='http://127.0.0.1/soap/server/server.wsdl'
xmlns:tns='http://127.0.0.1/soap/server/server.wsdl'
xmlns:xsd='http://www.w3.org/2001/XMLSchema'
xmlns:soap='http://schemas.xmlsoap.org/wsdl/soap/'
xmlns:soapenc='http://schemas.xmlsoap.org/soap/encoding/'
xmlns:wsdl='http://schemas.xmlsoap.org/wsdl/'
xmlns='http://schemas.xmlsoap.org/wsdl/'>
<wsdl:message name='getHelloWorldRequest'>
</wsdl:message>
<wsdl:message name='getHelloWorldResponse'>
<wsdl:part name='chaineRetour' type='xsd:string'/>
</wsdl:message>
<portType name='ERIC_PortType'>
<wsdl:operation name='getHelloWorld'>
<wsdl:input message='tns:getHelloWorldRequest'/>
<wsdl:output message='tns:getHelloWorldResponse'/>
</wsdl:operation>
</portType>
<binding name='ERIC_Binding' type='tns:ERIC_PortType'>
<soap:binding style='rpc'
transport='http://schemas.xmlsoap.org/soap/http' />
<wsdl:operation name='getHelloWorld'>
<soap:operation
soapAction='getHelloWorld'/>
<wsdl:input>
<soap:body use='literal' namespace='http://www.ericmery.fr/ns'/>
</wsdl:input>
<wsdl:output>
<soap:body use='literal' namespace='http://www.ericmery.fr/ns'/>
</wsdl:output>
</wsdl:operation>
</binding>
<service name='WebServiceFirst'>
<documentation>Web Service First</documentation>
<port name='ERIC_Port' binding='tns:ERIC_Binding'>
<soap:address location='http://127.0.0.1/soap/server/'/>
</port>
</service>
</definitions> |
crée le fichier WebServiceFirst.php
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| <?php
class WebServiceFirst {
/**
* Retourne la chaine 'Hello World'
*
* @access public
* @return string
*/
public function getHelloWorld() {
return "Hello World";
}
}
?> |
coté éclipse crée nouveau java project : onjava
crée nouveau package :onjava
crée une nouvelle classe CalcClient
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
| package onjava;
import java.io.*;
import java.net.*;
import java.util.*;
import org.apache.soap.*;
import org.apache.soap.encoding.SOAPMappingRegistry;
import org.apache.soap.encoding.soapenc.ArraySerializer;
import org.apache.soap.encoding.soapenc.StringDeserializer;
import org.apache.soap.rpc.*;
import org.apache.soap.util.xml.QName;
public class CalcClient {
public static void main(String[] args) throws Exception {
URL url = new URL ("http://localhost/soap/server/index.php");
// Creation de l'appel
Call call = new Call();
call.setTargetObjectURI("urn:WebServiceFirst");
call.setMethodName("getHelloWorld");
call.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);
//serialistation du type de retour en string
SOAPMappingRegistry soapMappingRegistry = new SOAPMappingRegistry();
soapMappingRegistry.mapTypes(Constants.NS_URI_SOAP_ENC, new QName("", "chaineRetour"),
null, null, new StringDeserializer());
call.setSOAPMappingRegistry(soapMappingRegistry);
// make the call: note that the action URI is empty because the
// XML-SOAP rpc router does not need this. This may change in the
// future.
//appel
Response resp = call.invoke(url, "" );
// verifier la réponse.
if ( resp.generatedFault() ) {
Fault fault = resp.getFault ();
System.out.println("The call failed: ");
System.out.println("Fault Code = " + fault.getFaultCode());
System.out.println("Fault String = " + fault.getFaultString());
}
else {
Parameter result = resp.getReturnValue();
System.out.println(result.getValue());
}
}
} |
pour que le code tourne il faut ajouter les jar suivant :
mail;soap;activation;xerces
bonne chance ;)