IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Bibliothèques et frameworks PHP Discussion :

[Web Service][SOAP]Retour d'appel à un web service pouvant contenir 0, 1, ou n éléments


Sujet :

Bibliothèques et frameworks PHP

  1. #1
    Membre confirmé Avatar de Satch
    Homme Profil pro
    Hypnothérapeute - Magicien
    Inscrit en
    Mars 2004
    Messages
    498
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 42
    Localisation : Suisse

    Informations professionnelles :
    Activité : Hypnothérapeute - Magicien

    Informations forums :
    Inscription : Mars 2004
    Messages : 498
    Points : 645
    Points
    645
    Par défaut [Web Service][SOAP]Retour d'appel à un web service pouvant contenir 0, 1, ou n éléments
    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 :
    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>
    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
    	$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;
    	}
    Par contre s'il n'y a qu'un seul employé le bout de code ci dessus ne fonctionne plus.
    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>

  2. #2
    Candidat au Club
    Inscrit en
    Janvier 2011
    Messages
    3
    Détails du profil
    Informations forums :
    Inscription : Janvier 2011
    Messages : 3
    Points : 2
    Points
    2
    Par défaut
    Moi j'ai contourné le problème comme cela :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
     
    	$client = new SoapClient("http://localhost/ws/EntrepriseService.asmx?wsdl");
    	$parametres = array('nombreEmployes'=>2);
     
    	$ret=$client->__soapCall('GetEntrepriseAvecEmployes', array($parametres));
    	$entreprise = $ret->GetEntrepriseAvecEmployesResult;
     
    	if (!is_array($entreprise->Employes->Employe)) $entreprise->Employes->Employe = array($entreprise->Employes->Employe);
    	foreach ($entreprise->Employes->Employe as $employe) {
    		echo $employe->Nom;
    	}

  3. #3
    Membre confirmé Avatar de Satch
    Homme Profil pro
    Hypnothérapeute - Magicien
    Inscrit en
    Mars 2004
    Messages
    498
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 42
    Localisation : Suisse

    Informations professionnelles :
    Activité : Hypnothérapeute - Magicien

    Informations forums :
    Inscription : Mars 2004
    Messages : 498
    Points : 645
    Points
    645
    Par défaut
    Merci pour la réponse.
    Ca me dérangeait de faire des if à tout bout de champ, mais j'ai en gros fait la même chose.

    J'ai pris un petit outil (wsdl2php) qui génère des classes php à partir du wsdl et l'ai modifié pour que une fonction toArray() soit incluse dans les objets problématiques.

    ça fonctionne à merveille maintenant.

    Mais bon, c'est quand même assez space que le soap de PHP fasse un objet ou un tableau d'objet suivant qu'il y ait un ou plusieurs éléments. Je ne trouve pas ça normal...

    quand on a :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
     <s:element minOccurs="0" maxOccurs="unbounded" name="Employe" nillable="true" type="tns:Employe" />
    pour moi il est clair qu'un tableau DOIT être retourné, peu importe le nombre d'employés.
    Mais ce n'est malheureusement pas le cas.

    En tous cas ça m'a rappelé pourquoi j'évitais de faire du PHP...

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [SOAP] problème d'appel à une fonction
    Par FrankyLeClown dans le forum XML/XSL et SOAP
    Réponses: 4
    Dernier message: 07/03/2011, 18h38
  2. JSF - Retour à l'appelant
    Par tofpele dans le forum Servlets/JSP
    Réponses: 0
    Dernier message: 18/11/2008, 16h23
  3. Réponses: 3
    Dernier message: 19/03/2008, 22h11
  4. [WebService][CFC/WSDL] Retour d'appel vide
    Par lzw1015bean dans le forum XML/XSL et SOAP
    Réponses: 1
    Dernier message: 06/12/2006, 11h16
  5. [SOAP] API pour appels asynchrones
    Par Dar Shak dans le forum API standards et tierces
    Réponses: 3
    Dernier message: 26/04/2005, 08h57

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo