How SOAP Works

written by: Emanuela Hedrick; article published: year 2006, month 08;


In: Categories » Internet » APIs and Web Feeds » How SOAP Works

A SOAP request will involve creating and populating a request envelope, which contains all the required information (as specified by the WSDL document), transmitting that envelope to the API server, and handling the response.

A SOAP request generally contains all of the following elements:

  • SOAP Envelope — With namespace inclusions.

  • SOAP Body — Possibly defining additional namespaces.

  • Desired Action — How the desired action is represented will depend on the API in question. It may be as simple as a parameter, or involve additional namespaces.

  • Developer Key — A unique identifier assigned by the server to the requestor.

  • Request Parameters — Detailing the request being performed.

With that information in mind, a SOAP request can be generated.

<?xmlversion="1.0" encoding=" UTF-8" standalone="no"?>
<SOAP-ENV:Envelope
 xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
 xmlns:xsd="http://www.w3.org/2001/XMLSchema"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <SOAP-ENV:Body>
    <devkey xsi:type="xsd:int">123</devkey>
    <action xsi:type="xsd:string">search</action>
    <type xsi:type="xsd:string">book</type>
    <keyword xsi:type="xsd:string">style</keyword>
  </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

The parameters included in the request are easy to pick out, and though the variable typing isn't of great importance for PHP development, it does come in handy for more strongly typed languages. The missing item here is the endpoint, made clear in the REST example because it was the URL to which the request was posted. SOAP requests, of course, are run against specified URIs, which do not need to be re-specified within the request itself.

The SOAP response would look like this:

<?xmlversion='1.0' encoding='UTF-8'?>
<SOAP-ENV:Envelope
  xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
 xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance"
 xmlns:xsd="http://www.w3.org/1999/XMLSchema">
 <SOAP-ENV:Body>
 <LibrarySearchResponsexmlns="http://library.example.com/api/ns">
  <RequestInfo>
    <devkey xsi:type=" xsd:string">123</devkey>
    <action xsi:type="xsd:string">search</action>
    <type xsi:type="xsd:string">book</type>
    <keyword xsi:type="xsd:string">style</keyword>
  </RequestInfo>
  <ResponseInfo>
   <ResultCount>2</ResultCount>
   <Item>
    <Title xsi:type="xsd:string">Style Book Vol 1</Title>
    <Status xsi:type="xsd:string">Out</Status>
    <Holds xsi:type="xsd:int">3</Holds>
    <CopiesOnHand xsi:type="xsd:int">2</CopiesOnHand>
    <Author xsi:type="xsd:string">Jon Doe</Author>
   </Item>
   <Item>
    <Title xsi:type="xsd:string">Style Book Vol 2</Title>
    <Status xsi:type="xsd:string">In</Status>
    <Holds xsi:type="xsd:int">0</Holds>
    <CopiesOnHand xsi:type="xsd:int">1</CopiesOnHand>
    <Author xsi:type="xsd:string">Jon Doe</Author>
   </Item>
  </ResponseInfo>
 </LibrarySearchResponse>
 </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

The SOAP response isn't too different from the REST response shown earlier. In fact, much of the name spacing could actually be omitted (though it is rare to see a SOAP response without it), at which point, with the exception of the additional encapsulation, the two documents would be very similar.

As you should be able to discern from the response shown, it declares itself to be XML 1.0 and uses UTF-8 for encoding. The SOAP-ENV:Envelope element is the root element for the document, and has threenamespaces, including the SOAP-ENV namespace. The Body then contains the LibrarySearchResponse element among other things, which also defines its own namespace.

The RequestInfo parent follows after, and this contains the request parameters that generated the response that follows on from there. Returning request parameters with the response is a common occurrence in SOAP.

Finally, the response itself is returned. Notice that the ResultCount element sits as a direct child of ResponseInfo, and the result items themselves are again stored under a repeating element, Item.

legal disclaimer

1) Our website is not responsible for the information contained by this article as well for any and all copyright infringements by authors and writers. E-articles is a free information resource. If you suspect this article for any copyright infringements, please read the Terms of service and contact us to investigate the problem.
2) The E-articles directory team is not responsible for inaccuracies, falsehoods, or any other types of misinformation this tutorial may contain and will not be liable for any loss or damage suffered by a user through the user's reliance on the information gained here. Please read the Terms of service

Useful tools and features

Translate this article to...    Send this article to you or to a friend

Link to this article from your page   
If you like this article (tutorial), please link to it from your web page using the information above. Linking to this page, this is the only way to help us improve our service, the same time providing your visitors with a way to improve their online experience.

related articles

1. Advantages and Disadvantages of SSL ~ API related
Configuring your web server to present the API over an SSL connection adds protection for both the request and response bodies, while requiring little to no additional coding for the API. Remember that the use of a server certificate only authenticates the server for the client, it does nothing to identify the client itself. It is best used layered with one of the previous two examples. Advantages: Encryption — Both request and response bodies are protected from intermediate prying eyes. ...

2. Why Do You Need to Produce Feeds
Feeds have several advantages, primarily related to consumption, over traditional HTML formats. Many desktop applications are devoted to reading feeds at regular intervals, and many of the new batch of web browsers include features for reading feeds. These free the user from manually checking various sources (websites) for new information. Instead, the automated tool checks the subscribed feeds every few minutes and presents them to the user (usually organized in a user-configurable manner). The standard and predictable format makes this a m...

3. How REST Works
Generally speaking, a REST request will involve sending a request to a special URL (similar to what you would see after filling out a form using the GET method), then receiving an XML document containing the server's response. The XML response is then parsed, and the desired information is extracted and acted upon. Each REST request generally has several common elements: Endpoint URL — The full address for the desired script. A REST service might have only a single script that handles all request type...

4. How to implement the REST technology
There are two sides to this tale, the first is how to generate legitimate REST requests, and the second is how to handle the responses correctly. Generating Requests When it comes to generating the request, you have three main options. First, you can generate the request manually, using PHP's header functions. This gives you complete flexibility in generating the request, but does involve the most coding. Second, you can use one of PHP's built-in request functions such as file_get_contents() or file()/fopen(), fread(...

5. Important Considerations When Using Feeds
XML feeds provide a great resource of information, but their use is not without its own special considerations. Security and legal concerns go hand in hand whether you are producing or consuming feeds. Consider if you will the implications of going away for the weekend, only to discover that your aggregator has been attacked, your site is now displaying wildly inaccurate information provided by the attacker, and your legal department is fielding not-so-nice phone calls regarding the current content of your homepage. Also consider how often...

6. Advantages and Disadvantages of Client Side Certificates
The API server can generate a certificate and provide it to the client via a secure channel before any requests are made. This certificate is then used in the authentication process; this confirms the identity of both the client and server before requests are made. Although this method provides the greatest level of security (barring a dedicated VPN connection, which won't be covered here), it also has the most strenuous requirements on both sides: not all modules (say, NuSOAP) can handle client-side certificates. Advantages:...

7. What are Feeds ~ RSS and ATOM Feed Specifications
You can think of feeds as small modules of information that can be plugged into existing websites, consumed by clients on their desktop, or consumed by aggregators to be presented by users with other feeds. Aggregators also offer searching functionality to users, allowing new users to locate your site and feed (a great reason to provide a feed in the first place). Websites such as Yahoo! produce web feeds. Software that downloads and uses feeds is said to consume or aggregate feeds. Sites such ...

8. Introduction to Web APIs ~ REST vs SOAP
When interacting with web services, generally the choice of which method to use will be made for you. The majority of services operate in either REST or SOAP, not both (Amazon is a notable exception to this rule). When given the choice, however, there are several points to consider: Overhead — REST requests are relatively slim. SOAP requests, on the other hand, contain a lot of additional information, which can really add up. Transparency — With REST requests (even when completed ov...

9. Common API Performance Techniques
Websites are designed to be accessed by individuals, and as such tend to rely on the relatively slow speed of the user to avoid any performance bottlenecks. This technique fails miserably with APIs because they are going to be consumed by other servers with high-speed connections, often designed only with their own performance in mind (they won't cache your responses for you, and will instead make exactly the same request time and time again). Designing your API with performance in mind can help keep the server fast even when many req...

10. Developing a Datafeed Strategy
What’s the best way to get started with your datafeed marketing? Have a datafeed party! What’s a datafeed party? As first reported in eBay Motors, a datafeed party does not refer to a party with vodka on the rocks, Sushi snacks, and square-dancing music. It refers to the process of sitting down and researching how many places you can send data feeds to sell in new marketplaces. So, have yourself a datafeed party and see how many new marketplaces you can find to sell your [items] with little additional effort. Sinc...