简体   繁体   中英

How to read a Dataset from Microsoft.NET SOAP Web Service in a PHP client

What is the best strategy to read a Dataset produced by a Microsoft .NET SOAP Web Service in a PHP client ?

I have the following response from the web service

<?xml version="1.0" encoding="UTF-8"?>
<DataSet xmlns="http://Example/wsExchangeExample/">
   <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" id="NewDataSet">
      <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
         <xs:complexType>
            <xs:choice minOccurs="0" maxOccurs="unbounded">
               <xs:element name="Table">
                  <xs:complexType>
                     <xs:sequence>
                        <xs:element name="Categoria" type="xs:string" minOccurs="0" />
                        <xs:element name="titolo" type="xs:string" minOccurs="0" />
                        <xs:element name="Cognome" type="xs:string" minOccurs="0" />
                        <xs:element name="Nome" type="xs:string" minOccurs="0" />
                        <xs:element name="Cod_fisc" type="xs:string" minOccurs="0" />
                        <xs:element name="DIP" type="xs:dateTime" minOccurs="0" />
                        <xs:element name="DPI" type="xs:dateTime" minOccurs="0" />
                        <xs:element name="DR" type="xs:dateTime" minOccurs="0" />
                        <xs:element name="L_PI" type="xs:string" minOccurs="0" />
                        <xs:element name="N_Iscrizione" type="xs:int" minOccurs="0" />
                        <xs:element name="d_canc" type="xs:dateTime" minOccurs="0" />
                        <xs:element name="D_nas" type="xs:dateTime" minOccurs="0" />
                        <xs:element name="L_nas" type="xs:string" minOccurs="0" />
                        <xs:element name="P_nascita" type="xs:string" minOccurs="0" />
                        <xs:element name="Scheda" type="xs:boolean" minOccurs="0" />
                        <xs:element name="Note" type="xs:string" minOccurs="0" />
                        <xs:element name="CodIscritto" type="xs:string" minOccurs="0" />
                        <xs:element name="Sesso" type="xs:string" minOccurs="0" />
                        <xs:element name="Stato" type="xs:string" minOccurs="0" />
                        <xs:element name="Senatore" type="xs:boolean" minOccurs="0" />
                        <xs:element name="Ultrasettantenne" type="xs:boolean" minOccurs="0" />
                        <xs:element name="Giovane" type="xs:boolean" minOccurs="0" />
                        <xs:element name="Pensionato" type="xs:boolean" minOccurs="0" />
                        <xs:element name="Dipendente" type="xs:boolean" minOccurs="0" />
                        <xs:element name="Carica" type="xs:string" minOccurs="0" />
                     </xs:sequence>
                  </xs:complexType>
               </xs:element>
            </xs:choice>
         </xs:complexType>
      </xs:element>
   </xs:schema>
   <diffgr:diffgram xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
      <NewDataSet xmlns="">
         <Table diffgr:id="Table1" msdata:rowOrder="0">
            <Categoria>B</Categoria>
            <titolo>Ing. Junior</titolo>
            <Cognome>BLACK</Cognome>
            <Nome>PETER</Nome>
            <Cod_fisc>AAAAAAAAAA</Cod_fisc>
            <DIP>2002-12-03T00:00:00+01:00</DIP>
            <DPI>2002-12-03T00:00:00+01:00</DPI>
            <L_PI>MILANO</L_PI>
            <N_Iscrizione>1</N_Iscrizione>
            <D_nas>1971-03-08T00:00:00+01:00</D_nas>
            <L_nas>Borgo Sesia</L_nas>
            <P_nascita>MI</P_nascita>
            <Scheda>false</Scheda>
            <CodIscritto>1B</CodIscritto>
            <Sesso>1</Sesso>
            <Stato>Attivo</Stato>
            <Senatore>false</Senatore>
            <Ultrasettantenne>false</Ultrasettantenne>
            <Giovane>false</Giovane>
            <Pensionato>false</Pensionato>
            <Dipendente>true</Dipendente>
         </Table>
         <Table diffgr:id="Table2" msdata:rowOrder="1">
            <Categoria>B</Categoria>
            <titolo>Ing. Junior</titolo>
            <Cognome>WHITE</Cognome>
            <Nome>CAROL</Nome>
            <Cod_fisc>XXXXXXXXX</Cod_fisc>
            <DIP>2003-01-14T00:00:00+01:00</DIP>
            <DPI>2003-01-14T00:00:00+01:00</DPI>
            <L_PI>Como</L_PI>
            <N_Iscrizione>9999</N_Iscrizione>
            <D_nas>1975-03-09T00:00:00+01:00</D_nas>
            <L_nas>Como</L_nas>
            <Scheda>false</Scheda>
            <CodIscritto>2B</CodIscritto>
            <Sesso>2</Sesso>
            <Stato>Attivo</Stato>
            <Senatore>false</Senatore>
            <Ultrasettantenne>false</Ultrasettantenne>
            <Giovane>false</Giovane>
            <Pensionato>false</Pensionato>
            <Dipendente>false</Dipendente>
         </Table>
      </NewDataSet>
   </diffgr:diffgram>
</DataSet>

My PHP code so far is very simple:

/* Initialize webservice with your WSDL */
$client = new SoapClient("http://www.myurl.com/wsExchangeExample/wservice.asmx?WSDL");

/* Invoke webservice method with your parameters, in this case: Function1 */
$response = $client->__soapCall("Function1", array());

/* Print webservice response */
var_dump($response);

It returns something like:

object(stdClass)[2]
  public 'Function1Result' => 
    object(stdClass)[3]
      public 'schema' => string '<xs:schema xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" id="NewDataSet"><xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true"><xs:complexType><xs:choice minOccurs="0" maxOccurs="unbounded"><xs:element name="Table"><xs:complexType><xs:sequence><xs:element name="Categoria" type="xs:string" minOccurs="0"/><xs:element name="titolo" type="xs:string" minOccurs="0"/><xs:element name="Cognome" type="xs:string" minOccurs="0"/>'... (length=1916)
      public 'any' => string '<diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1"><NewDataSet xmlns=""><Table diffgr:id etc. etc.

How it is possible to parse this response ? I have tried the following:

$xml = new SimpleXMLElement($response);

but I get an error ($response is not a string). So, which is the best way to perform this task in PHP ?

You have an stdClass instance, read more here . You can read its RitornaTabellaConfineResult using $response->RitornaTabellaConfineResult . However, you might want to convert this into an array . A way to do it is a combination of json_decode and json_encode :

$responseArray = json_decode(json_encode($response), true);
//Test
echo var_dump($responseArray);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM