如何使用PHP通过SOAP请求将XML和Schema作为参数传递?

时间:2019-02-04 22:05:45

标签: php xml soap xsd wsdl

我需要能够通过SOAP API“添加引号”,这需要XML和Schema作为请求中的参数传递。

对于其他请求,我正在使用PHP的SoapClient,但不确定如何格式化此请求?为了测试我尝试过通过PHP中的Curl发送请求的方法,它可以工作,但是想知道如何使用SoapClient ...有人可以在这里帮助我吗?非常感谢。

以下是SOAP 1.1请求和响应的示例。显示的占位符需要替换为实际值(例如,长度,字符串,架构,xml ...)

请求:

POST /abc/Quote.asmx HTTP/1.1
Host: myserver
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://example.com/AddQuote"

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <AddQuote xmlns="http://example.com">
      <connectId>string</connectId>
      <dsQuote>
        <xsd:schema>schema</xsd:schema>xml</dsQuote>
    </AddQuote>
  </soap:Body>
</soap:Envelope>

响应:

HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
 <soap:Body>
   <AddQuoteResponse xmlns="http://example.com">
     <AddQuoteResult>string</AddQuoteResult>
     <dtMessage>xmlxml</dtMessage>
   </AddQuoteResponse>
 </soap:Body>

这是WSDL:

<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://example.com" xmlns:s="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://example.com">
<wsdl:types>
<s:schema elementFormDefault="qualified" targetNamespace="http://example.com">
<s:element name="AddQuote">
<s:complexType>
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="connectId" type="s:string"/>
<s:element minOccurs="0" maxOccurs="1" name="dsQuote">
<s:complexType>
<s:sequence>
<s:element ref="s:schema"/>
<s:any/>
</s:sequence>
</s:complexType>
</s:element>
</s:sequence>
</s:complexType>
</s:element>
<s:element name="AddQuoteResponse">
<s:complexType>
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="AddQuoteResult" type="s:string"/>
<s:element minOccurs="0" maxOccurs="1" name="dtMessage">
<s:complexType>
<s:sequence>
<s:any minOccurs="0" maxOccurs="unbounded" namespace="http://www.w3.org/2001/XMLSchema" processContents="lax"/>
<s:any minOccurs="1" namespace="urn:schemas-microsoft-com:xml-diffgram-v1" processContents="lax"/>
</s:sequence>
</s:complexType>
</s:element>
</s:sequence>
</s:complexType>
</s:element>
</s:schema>
</wsdl:types>
<wsdl:message name="AddQuoteSoapIn">
<wsdl:part name="parameters" element="tns:AddQuote"/>
</wsdl:message>
<wsdl:message name="AddQuoteSoapOut">
<wsdl:part name="parameters" element="tns:AddQuoteResponse"/>
</wsdl:message>
<wsdl:portType name="QuoteSoap">
<wsdl:operation name="AddQuote">
<wsdl:input message="tns:AddQuoteSoapIn"/>
<wsdl:output message="tns:AddQuoteSoapOut"/>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="QuoteSoap" type="tns:QuoteSoap">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="AddQuote">
<soap:operation soapAction="http://example.com/AddQuote" 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="QuoteSoap12" type="tns:QuoteSoap">
<soap12:binding transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="AddQuote">
<soap12:operation soapAction="http://example.com/AddQuote" 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="Quote">
<wsdl:port name="QuoteSoap" binding="tns:QuoteSoap">
<soap:address location="http://myserver/abc/Quote.asmx"/>
</wsdl:port>
<wsdl:port name="QuoteSoap12" binding="tns:QuoteSoap12">
<soap12:address location="http://myserver/abc/Quote.asmx"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>

这是一个外观示例(从另一个执行相同功能的应用程序复制):

<?xml version="1.0"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:mar="http://example.com" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <soap:Header/>
  <soap:Body>
    <mar:AddQuote>
      <mar:connectId>71a0-0e82-4c14-5bcb58bf9701-wer4</mar:connectId>
      <mar:dsQuote>
        <xs:schema id="CustomerQuote" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:msprop="urn:schemas-microsoft-com:xml-msprop">
          <xs:element name="CustomerQuote" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
            <xs:complexType>
              <xs:choice minOccurs="0" maxOccurs="unbounded">
                <xs:element name="CustomerQuote">
                  <xs:complexType>
                    <xs:sequence>
                      <xs:element name="QuoteID" type="xs:string" minOccurs="0"/>
                      <xs:element name="QuoteStatus" type="xs:string" minOccurs="0"/>
                      <xs:element name="CustomerID" type="xs:string" minOccurs="0"/>
                      <xs:element name="Reference" type="xs:string" minOccurs="0"/>
                      <xs:element name="SalesEntity" type="xs:string" minOccurs="0"/>
                      <xs:element name="WarehouseEntity" type="xs:string" minOccurs="0"/>
                      <xs:element name="EnterDate" type="xs:string" minOccurs="0"/>
                      <xs:element name="EnterTime" type="xs:string" minOccurs="0"/>
                      <xs:element name="ShipDate" type="xs:string" minOccurs="0"/>
                      <xs:element name="ExpiryDate" type="xs:string" minOccurs="0"/>
                      <xs:element name="Terms" type="xs:string" minOccurs="0"/>
                      <xs:element name="ExtendedEx" type="xs:string" minOccurs="0"/>
                      <xs:element name="TaxTotal" type="xs:string" minOccurs="0"/>
                      <xs:element name="ExtendedInc" type="xs:string" minOccurs="0"/>
                      <xs:element name="Currency" type="xs:string" minOccurs="0"/>
                      <xs:element name="SalesRep" type="xs:string" minOccurs="0"/>
                      <xs:element name="TaxNumber" type="xs:string" minOccurs="0"/>
                      <xs:element name="TaxClass" type="xs:string" minOccurs="0"/>
                      <xs:element name="ExchangeRate" type="xs:string" minOccurs="0"/>
                      <xs:element name="QuoteTransactionType" type="xs:string" minOccurs="0"/>
                      <xs:element name="Category1" type="xs:string" minOccurs="0"/>
                      <xs:element name="Category2" type="xs:string" minOccurs="0"/>
                      <xs:element name="Category3" type="xs:string" minOccurs="0"/>
                      <xs:element name="Category4" type="xs:string" minOccurs="0"/>
                      <xs:element name="Category5" type="xs:string" minOccurs="0"/>
                      <xs:element name="Category6" type="xs:string" minOccurs="0"/>
                      <xs:element name="Special1" type="xs:string" minOccurs="0"/>
                      <xs:element name="Special2" type="xs:string" minOccurs="0"/>
                      <xs:element name="NumLines" type="xs:string" minOccurs="0"/>
                      <xs:element name="Completed" type="xs:string" minOccurs="0"/>
                      <xs:element name="SiteCode" type="xs:string" minOccurs="0"/>
                      <xs:element name="Name" type="xs:string" minOccurs="0"/>
                      <xs:element name="Address1" type="xs:string" minOccurs="0"/>
                      <xs:element name="Address2" type="xs:string" minOccurs="0"/>
                      <xs:element name="City" type="xs:string" minOccurs="0"/>
                      <xs:element name="State" type="xs:string" minOccurs="0"/>
                      <xs:element name="PostalCode" type="xs:string" minOccurs="0"/>
                      <xs:element name="Country" type="xs:string" minOccurs="0"/>
                      <xs:element name="DeliveryName" type="xs:string" minOccurs="0"/>
                      <xs:element name="DeliveryAddress1" type="xs:string" minOccurs="0"/>
                      <xs:element name="DeliveryAddress2" type="xs:string" minOccurs="0"/>
                      <xs:element name="DeliveryCity" type="xs:string" minOccurs="0"/>
                      <xs:element name="DeliveryState" type="xs:string" minOccurs="0"/>
                      <xs:element name="DeliveryPostalCode" type="xs:string" minOccurs="0"/>
                      <xs:element name="DeliveryCountry" type="xs:string" minOccurs="0"/>
                      <xs:element name="FreightType" type="xs:string" minOccurs="0"/>
                      <xs:element name="FreightAccount" type="xs:string" minOccurs="0"/>
                      <xs:element name="Carrier" type="xs:string" minOccurs="0"/>
                      <xs:element name="Shipping" type="xs:string" minOccurs="0"/>
                      <xs:element name="Contact" type="xs:string" minOccurs="0"/>
                      <xs:element name="Email" type="xs:string" minOccurs="0"/>
                      <xs:element name="Phone" type="xs:string" minOccurs="0"/>
                      <xs:element name="Fax" type="xs:string" minOccurs="0"/>
                      <xs:element name="Territory" type="xs:string" minOccurs="0"/>
                      <xs:element name="Comment" type="xs:string" minOccurs="0"/>
                      <xs:element name="PriceCategory" type="xs:string" minOccurs="0"/>
                      <xs:element name="IsOpen" type="xs:string" minOccurs="0"/>
                      <xs:element name="Expired" type="xs:string" minOccurs="0"/>
                      <xs:element name="Accepted" type="xs:string" minOccurs="0"/>
                    </xs:sequence>
                  </xs:complexType>
                </xs:element>
                <xs:element name="CustomerQuoteLine">
                  <xs:complexType>
                    <xs:sequence>
                      <xs:element name="QuoteID" type="xs:string" minOccurs="0"/>
                      <xs:element name="LineNumber" type="xs:string" minOccurs="0"/>
                      <xs:element name="LineNumberReference" type="xs:string" minOccurs="0"/>
                      <xs:element name="LineType" type="xs:string" minOccurs="0"/>
                      <xs:element name="ProductID" type="xs:string" minOccurs="0"/>
                      <xs:element name="Barcode" type="xs:string" minOccurs="0"/>
                      <xs:element name="ProductCode" type="xs:string" minOccurs="0"/>
                      <xs:element name="ProductGroup" type="xs:string" minOccurs="0"/>
                      <xs:element name="Description" type="xs:string" minOccurs="0"/>
                      <xs:element name="UnitCode" type="xs:string" minOccurs="0"/>
                      <xs:element name="CustomerProductCode" type="xs:string" minOccurs="0"/>
                      <xs:element name="Quantity" type="xs:string" minOccurs="0"/>
                      <xs:element name="QuantityReserve" type="xs:string" minOccurs="0"/>
                      <xs:element name="QuantityKit" type="xs:string" minOccurs="0"/>
                      <xs:element name="BasePrice" type="xs:string" minOccurs="0"/>
                      <xs:element name="Price" type="xs:string" minOccurs="0"/>
                      <xs:element name="PriceCurr" type="xs:string" minOccurs="0"/>
                      <xs:element name="Discount" type="xs:string" minOccurs="0"/>
                      <xs:element name="UnitPrice" type="xs:string" minOccurs="0"/>
                      <xs:element name="FixedPrice" type="xs:string" minOccurs="0"/>
                      <xs:element name="SpecialPrice" type="xs:string" minOccurs="0"/>
                      <xs:element name="TaxIncluded" type="xs:string" minOccurs="0"/>
                      <xs:element name="PricePer" type="xs:string" minOccurs="0"/>
                      <xs:element name="Override" type="xs:string" minOccurs="0"/>
                      <xs:element name="ExtendedEx" type="xs:string" minOccurs="0"/>
                      <xs:element name="TaxTotal" type="xs:string" minOccurs="0"/>
                      <xs:element name="ExtendedInc" type="xs:string" minOccurs="0"/>
                      <xs:element name="Comment" type="xs:string" minOccurs="0"/>
                      <xs:element name="ShipDate" type="xs:string" minOccurs="0"/>
                      <xs:element name="ExpiryDate" type="xs:string" minOccurs="0"/>
                    </xs:sequence>
                  </xs:complexType>
                </xs:element>
              </xs:choice>
            </xs:complexType>
          </xs:element>
        </xs:schema>
        <diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
          <CustomerQuote>
            <CustomerQuote diffgr:id="CustomerQuote1" msdata:rowOrder="0">
              <QuoteID>123456</QuoteID>
              <QuoteStatus>SUBMITTED</QuoteStatus>
              <CustomerID>555555</CustomerID>
              <Reference>123456</Reference>
              <SalesEntity>999</SalesEntity>
              <Currency>AUD</Currency>
              <NumLines>1</NumLines>
              <Contact>mrsmith@example.com</Contact>
              <Email>mrsmith@example.com</Email>
              <Phone/>
              <Comment>Some comment here</Comment>
            </CustomerQuote>
            <CustomerQuoteLine diffgr:id="CustomerQuoteLine1" msdata:rowOrder="0">
              <QuoteID>123456</QuoteID>
              <LineNumber>1</LineNumber>
              <ProductID>3898</ProductID>
              <Description>BIG BROWN BOX</Description>
              <UnitCode>EA</UnitCode>
              <Quantity>1</Quantity>
              <Price>9.99</Price>
              <PriceCurr>9.99</PriceCurr>
            </CustomerQuoteLine>
          </CustomerQuote>
        </diffgr:diffgram>
      </mar:dsQuote>
    </mar:AddQuote>
  </soap:Body>
</soap:Envelope>

0 个答案:

没有答案