In: Categories » Internet » APIs and Web Feeds » How to implement the SOAP technology
| Like REST, implementing SOAP involves both generating requests and then handling the response. Whereas handling the SOAP response is similar to the REST result, generating the SOAP request is quite different. Generating RequestsUnlike REST, it is rather uncommon to see requests generated manually, though it can still be done. Generally, SOAP requests are either generated with a generic tool (like NuSOAP or Pear:SOAP) or with an application-specific class or module. Manual generation is covered here (a good understanding of how it works will come in useful), as well as NuSOAP. Application-specific tools are going to be ignored, for a couple reasons. By definition they are application specific, and therefore not very useful in this generic article. Secondly, the tools have varying interfaces and functionality levels, and in order to do the topic justice it would require an in-depth look. Finally, I am firmly of the opinion that if you can figure out how to make it work with NuSOAP (or raw, for that matter), you can get the application-specific tool to work too. Manual GenerationGenerating SOAP requests manually isn't too different from generating REST requests. The process of generating the request and actually transmitting it is split into two separate functions, for demonstrating purposes. For generating the request itself, I have chosen to use a pregenerated string, and merely populate the required values at runtime. There are more complex options (such as creating the document within SimpleXML, or creating it from scratch each run), but they aren't really required: functioncreateRequest($devkey, $action, $type, $keyword) As you can tell, the function is just about as simple as it can get. Note that the variables haven't been URL-encoded; that's because they aren't being sent in the URL (sounds obvious, but it's also easy to miss). Actually, calling the API to transmit the request involves borrowing some code from the first REST example, because the request will be sent raw this time. functioncallSOAPAPI($data) I could have just populated the $host and $path variables at the start, but this should be clearer. In a production system, you could save a few CPU cycles by hardcoding these elements. $fp=fsockopen($host, 80); This block is nearly identical to the previous example. The previous example was a POST request, whereas this is a GET request, a change that will be dictated by whatever API you are working with. The Content-Type header is different to accurately reflect what you're sending and this time of course you have $data to send, so Content-Length won't be 0. $response=""; This section is also identical to the REST example. Wrapping both those functions to retrieve a response is only barely worth mentioning: $request =createRequest('123', 'search', 'book','style); Wasn't too hard! Unlike REST, you can't just use file_get_contents() to hit the API, because you need to send the XML body with the request. file_put_contents() wont work either, because you need the response (file_put_contents() returns an int), but there are other options. Pear (http://pear.php.net), for example, has a bunch of HTTP-specific functions that can take some of the headache of manually creating the request off your hands, but still allow you all the flexibility you get with manual creation. Generation with NuSOAPFor anything other than a one-shot program, I would definitely recommend going with some sort of a SOAP module to make your coding life easier. Although, if you run into problems, tracking them down can be a bit of a pain. I like using a local development box during development, so should things start going awry I can use a packet sniffer to look at the request/response in its raw form. Conceptually, using NuSOAP isn't too different from completing the task manually. The object is initialized, the payload is created, and the request is sent. The key difference here is that NuSOAP is doing all the dirty work. require('../lib/nusoap.php'); Here the $client object is created. Two options are available when creating a new soapclient: You can either specify the wsdl file for the service (and set the second parameter to true), or specify the endpoint for the call (and set the second parameter to false). Whenever possible, I like to use the wsdl file; the NuSOAP module can catch some of your mistakes that way, and it ensures that different request types all go to the correct endpoint. $params =array( Preparing the parameters for transmission is a bit easier than earlier methods. $namespace= 'http://library.example.com'; Finally, the last few parameters are set, and the call itself is made. Handling the ResponseHandling a response from a SOAP request is again not too different from the REST response—both are provided in similar XML formats. The SOAP response carries the additional Envelope and Body elements, but often present data in a similar manner within those elements. There is of course some variation between handling the response from a manual request and from a NuSOAP request. Both methods are presented here. Manually Parsing the ResponseMimicking the output generated with the REST request uses similar code, with a few modifications for the encapsulation scheme used with SOAP. echo"You searched for:{$xml->Body->LibrarySearchResponse->RequestInfo-> echo"Here are your{$xml->Body->LibrarySearchResponse->ResponseInfo- >ResultCount} This will generate identical output as the REST request shown earlier. Note the different syntax used to acquire the search keyword. It isn't an attribute this time, so access is different. Parsing the Response with NuSOAPAccessing the object provided by NuSOAP is a little different from the methods used with SimpleXML, but the internal data structure is quite similar. Modifying the code to work with the NuSOAP object only takes a few moments. echo"You searched for: ". With NuSOAP, the internal data is accessed much the same way as an associative array, so rather than the OO method of using -> to access child elements, further array information is included. This element will output identically to the previous example. SOAP is an effective method of querying APIs when the additional overhead is permissible. The encapsulation of all elements allows for easy reading, and variable scope within the request. Creating SOAP requests can be as easy as writing them out once, then just replacing key variables. Alternatively, it can also be accomplished by using a tool such as NuSOAP. Accessing the SOAP response can be accomplished in much the same manner as the REST response when the request has been completed manually. In the case of requests completed with NuSOAP, the access method is structurally identical, with only a few minor changes to the syntax used.
|
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
related articles
The two primary architectures for APIs are REST and SOAP. When creating your API, you really have three options: REST, SOAP, or both. REST APIs are known for being easy and quick to develop for, but the entire request is sent in the clear regardless of the type of encryption used. SOAP APIs are more complex, requiring more effort to generate the response and handle the request, but allow for greater flexibility by adding namespace support. Providing APIs of both types may sound like an attractive option, but keep in mind that it will double ...
2. 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. ...
3. 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 depen...
4. 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...
5. 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...
6. 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(...
7. 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...
8. 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:...
9. 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 ...
10. 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...










