如何在 PHP 客户端中从 Microsoft.NET SOAP Web 服务读取数据集



PHP客户端中读取由Microsoft.NET SOAP Web服务生成的数据集的最佳策略是什么?

我有来自网络服务的以下响应

<?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>

到目前为止,我的PHP代码非常简单:

/* 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);

它返回如下内容:

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.

如何解析此响应?我尝试了以下方法:

$xml = new SimpleXMLElement($response);

但我收到一个错误($response不是字符串(。那么,在PHP中执行此任务的最佳方法是什么?

您有一个stdClass实例,请在此处阅读更多内容。您可以使用$response->RitornaTabellaConfineResult读取其RitornaTabellaConfineResult。但是,您可能希望将其转换为array。一种方法是将json_decode和json_encode结合起来:

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

相关内容

  • 没有找到相关文章

最新更新