We learned that our Drupal development site can act as a web services client or a web services provider (or server). Drupal does this by integrating and using various types of web services code, including XML-RPC, REST, JSON, XML, and RSS. In this chapter, we're going to discuss how our Drupal site can act as a web services consumer. This means that we can take our Drupal site and add code to it in the form of a Drupal module, and make this module connect to an external server or application that provides a web service that we want to utilize and consume data from.
We're first going to look in detail at a protocol that Drupal can use to make this communication happen. This web services protocol is called SOAP (Simple Object Access Protocol). We'll define SOAP and look at various implementations of
SOAP with our Drupal site. We'll also look in detail at the two contributed Drupal modules that utilize SOAP for web service integration. The SOAP Client module is a developer's module and has a higher learning curve. It assumes you know how to write your own web services remote procedure call code and implement this code in a custom module. You can then use the SOAP Client module to enable your custom module to talk to an external web service and load its WSDL (Web Service Description Language), for example.
WSDL is an XML-based document(s) that describes and outlines a web service and the model for interacting with this web service. The document specifies where the web service resides, and the types of operations the web service performs. This includes specifications defining the web service, port, or the endpoint where the service is located; the binding style; port type or interface; operations performed; and the message corresponding to this operation. For more on WSDL, see the definition at Wikipedia: http://en.wikipedia.org/wiki/Web_Services_ Description_Language.
We'll also look in detail at a contributed module that has already been coded for us but provides a large amount of functionality, especially for Drupal sites running
e-c commerce using Ubercart. The module is the FedEx Shipping Quotes for Ubercart module and it allows our Drupal site to consume web services from the FedEx Shipping Services API. For example, this module allows your site visitors to get real-time FedEx shipping quotes returned to them when they proceed to the checkout from their Ubercart order. With their products in their shopping cart, the site visitor can get shipping quotes immediately in their checkout screen and the quotes are
up-to-date from the FedEx Web Services. Shoppers can then easily select the quote they want to use with their order, for example, FedEx Overnight shipping, and this will automatically add the shipping cost to their Ubercart order.
This module will give you a good practical and easy-to-use example of consuming web services in Drupal.
So to summarize, in this chapter we will:
· Define SOAP and determine how we can use SOAP with Drupal
· Use the SOAP Client module
· Use the FedEx Shipping Quotes module
Using SOAP
SOAP (Simple Object Access Protocol) is an object-access protocol that allows us
to exchange information within our web services environment. The SOAP protocol is currently maintained by the XML Protocol Working Group of the World Wide Web Consortium (W3C). The current SOAP version is 1.2.
SOAP works by making a Remote Procedure Call to an application on an external server. In this chapter, we're going to look at an example of SOAP being used in a Drupal site to make a call from an Ubercart installation on your Drupal site to an external FedEx Shipping API to request shipping quotes for a customer's product that is in their shopping cart on the Drupal site. So, SOAP will enable this call to happen, and for Drupal to consume the resulting shipping quote.
The way the call works is that it is encoded in XML format by the SOAP client script. XML was chosen as the format to use for the messaging, as it's a standard markup language used by many major corporations and open source applications.
The XML is then sent over the HTTP transport protocol to the external web application server. HTTP is a popular method of transporting the messages encoded by the SOAP client and server because HTTP is the common transport method used by many networks and firewalls. SOAP can also be used with HTTPS for any secure transactions, for example, when you are using Ubercart to pass client data over to the FedEx server API. These transactions should be kept secure so that SOAP supports this secure framework.
In our FedEx example, the FedEx server returns an XML-formatted document with shipping quote data based on the remote procedure call that was submitted by the Drupal client. This data gets integrated directly into our Ubercart installation using a contributed Ubercart FedEx API module that integrates with the SOAP protocol. We're going to look at both this Ubercart module and the Drupal-contributed SOAP Client module in this chapter.
Drupal uses two types of SOAP with its web services consumption. Drupal supports the PHP 5.x SOAP extension (through various downloaded package versions) and also the NuSOAP extension. NuSOAP is a not a SOAP extension but rather a set
of PHP classes that allow for the creation and consumption of web services. You do not need to install any special PHP extensions if you run NuSOAP. NuSOAP does support the SOAP 1.1 specification and can generate WSDL documents. In this chapter, we're going to focus on using the PHP SOAP extension rather than
NuSOAP, but you now know that there is another option for using SOAP with your server and site.
For more on the history and functionality of SOAP, see the following articles: SOAP Wikipedia article (http://en.wikipedia.org/wiki/SOAP), SOAP specification, and latest versions (http://www.w3.org/TR/soap/), using SOAP with PHP through Mac Developer Connection (http://developer.apple.com/internet/webservices/soapphp.html).
Comments
Post a Comment