Bonjour,
Je sèche depuis quelques heures sur un problème dont je commence à me demander s'il a une solution simple...
J'ai réduit le problème à son plus simple appareil :
Un web service qui me renvoie une entreprise avec un certain nombre d'employé.
Ce web service me renvoie ceci :
Lorsqu'il y a plusieurs employés, je peux en parcourir la liste de cette manière :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14 <Entreprise> <Nom>entreprise</Nom> <Employes> <Employe> <Nom>nomEmployé 0</Nom> <Prenom>prenomEmploye 0</Prenom> </Employe> <Employe> <Nom>nomEmployé 1</Nom> <Prenom>prenomEmploye 1</Prenom> </Employe> </Employes> </Entreprise>
Par contre s'il n'y a qu'un seul employé le bout de code ci dessus ne fonctionne plus.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9 $client = new SoapClient("http://localhost/ws/EntrepriseService.asmx?wsdl"); $parametres = array('nombreEmployes'=>2); $ret=$client->__soapCall('GetEntrepriseAvecEmployes', array($parametres)); $entreprise = $ret->GetEntrepriseAvecEmployesResult; foreach ($entreprise->Employes->Employe as $employe) { echo $employe->Nom; }
je suis obligé de faire $entreprise->Employes au lieu de $entreprise->Employes->Employe dans le foreach.
D'après ce que j'en ai compris, lorsque dans l'élément <Employes> il y a plusieurs <Employe> le soapClient créé un tableau avec les éléments dedans, mais pas dans le cas ou il n'y en a qu'un seul. Dans ce cas, il renvoie l'élément directement.
Y a-t-il un moyen simple de contourner ça ??
PS : le WSLD, au cas ou
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90<?xml version="1.0" encoding="utf-8"?> <wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tm="http://microsoft.com/wsdl/mime/textMatching/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:tns="http://tempuri.org/" xmlns:s="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" targetNamespace="http://tempuri.org/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"> <wsdl:types> <s:schema elementFormDefault="qualified" targetNamespace="http://tempuri.org/"> <s:element name="GetEntrepriseAvecEmployes"> <s:complexType> <s:sequence> <s:element minOccurs="1" maxOccurs="1" name="nombreEmployes" type="s:int" /> </s:sequence> </s:complexType> </s:element> <s:element name="GetEntrepriseAvecEmployesResponse"> <s:complexType> <s:sequence> <s:element minOccurs="0" maxOccurs="1" name="GetEntrepriseAvecEmployesResult" type="tns:Entreprise" /> </s:sequence> </s:complexType> </s:element> <s:complexType name="Entreprise"> <s:sequence> <s:element minOccurs="0" maxOccurs="1" name="Nom" type="s:string" /> <s:element minOccurs="0" maxOccurs="1" name="Employes" type="tns:ArrayOfEmploye" /> </s:sequence> </s:complexType> <s:complexType name="ArrayOfEmploye"> <s:sequence> <s:element minOccurs="0" maxOccurs="unbounded" name="Employe" nillable="true" type="tns:Employe" /> </s:sequence> </s:complexType> <s:complexType name="Employe"> <s:sequence> <s:element minOccurs="0" maxOccurs="1" name="Nom" type="s:string" /> <s:element minOccurs="0" maxOccurs="1" name="Prenom" type="s:string" /> </s:sequence> </s:complexType> </s:schema> </wsdl:types> <wsdl:message name="GetEntrepriseAvecEmployesSoapIn"> <wsdl:part name="parameters" element="tns:GetEntrepriseAvecEmployes" /> </wsdl:message> <wsdl:message name="GetEntrepriseAvecEmployesSoapOut"> <wsdl:part name="parameters" element="tns:GetEntrepriseAvecEmployesResponse" /> </wsdl:message> <wsdl:portType name="EntrepriseServiceSoap"> <wsdl:operation name="GetEntrepriseAvecEmployes"> <wsdl:input message="tns:GetEntrepriseAvecEmployesSoapIn" /> <wsdl:output message="tns:GetEntrepriseAvecEmployesSoapOut" /> </wsdl:operation> </wsdl:portType> <wsdl:binding name="EntrepriseServiceSoap" type="tns:EntrepriseServiceSoap"> <soap:binding transport="http://schemas.xmlsoap.org/soap/http" /> <wsdl:operation name="GetEntrepriseAvecEmployes"> <soap:operation soapAction="http://tempuri.org/GetEntrepriseAvecEmployes" style="document" /> <wsdl:input> <soap:body use="literal" /> </wsdl:input> <wsdl:output> <soap:body use="literal" /> </wsdl:output> </wsdl:operation> </wsdl:binding> <wsdl:binding name="EntrepriseServiceSoap12" type="tns:EntrepriseServiceSoap"> <soap12:binding transport="http://schemas.xmlsoap.org/soap/http" /> <wsdl:operation name="GetEntrepriseAvecEmployes"> <soap12:operation soapAction="http://tempuri.org/GetEntrepriseAvecEmployes" style="document" /> <wsdl:input> <soap12:body use="literal" /> </wsdl:input> <wsdl:output> <soap12:body use="literal" /> </wsdl:output> </wsdl:operation> </wsdl:binding> <wsdl:service name="EntrepriseService"> <wsdl:port name="EntrepriseServiceSoap" binding="tns:EntrepriseServiceSoap"> <soap:address location="http://localhost/ws/EntrepriseService.asmx" /> </wsdl:port> <wsdl:port name="EntrepriseServiceSoap12" binding="tns:EntrepriseServiceSoap12"> <soap12:address location="http://localhost/ws/EntrepriseService.asmx" /> </wsdl:port> </wsdl:service> </wsdl:definitions>
Partager