Home > Articles > Web Services > XML

  • Print
  • + Share This
This chapter is from the book

This chapter is from the book

Other Sources of Input

Note that this input can easily be modified to come from a source other than the command line. For example, let's say that you have a database containing book information and have all the fields in the XML document except the price. So, you can easily use the Perl DBI to walk through the database, extract the ISBN of each book, and use the web service to retrieve the price of each book.

10–15 In this section of the application, we instantiate a SOAP::Lite object and actually send a request to the web service. This application is very similar to the SOAP client that accessed the Weather-Temperature service back in Listing 10.1. We first call the uri() method and specify the namespace, or in our case, the Perl module we are invoking. Next, we specify the location of the web service server script using proxy() method. At this point, we're ready to actually invoke the getInfo() method and pass in the $isbn variable.

17–26 After calling the getInfo() method, we check the return value (named $result) to see if any faults were returned by the SOAP server. If no faults were returned, we print the returned values from the getInfo() method. If a fault was returned, we print out the following fault information: faultcode, faultstring, faultfactor, and faultdetail. These fault elements were discussed earlier in this chapter.

Running the Web Service

After we have developed all the components that comprise the web service, we need to install it onto a machine. However, before we can offer a web service, we need to have a web server installed and listening on the default port 80. After installing the web server, we place our SOAP server script shown in Listing 10.10 (ch10_book_service.cgi) into the cgi-bin directory of the web server.


For testing in this chapter, we used the Apache web server. The Apache web server from the Apache Software foundation is an open source product and available for free download from http://httpd.apache.org. Note that if you're using a different web server, our web service setup and installation instructions will have to be adjusted.

Any web server should work for this example as long as it supports CGI scripts. However, the instructions for installation may not apply to different web servers. I assumed the scripts will be placed into the cgi-bin directory, but any directory capable of executing CGI scripts is fine as long as you modify the proxy() call argument in all the scripts that we've created and pass the correct URL to the SOAP server. The rest of the installation instructions assume that you have a cgi-bin directory and are running the Apache web server with default installation settings.

  1. Place the files from listings 10.8 (ch10_book_inventory.xml), 10.9 (Books.pm) and 10.10 (book_service.cgi) into the cgi-bin directory. Based on the default installation, the cgi-bin directory is located in the root Apache installation folder.

  2. Verify that files ending with a .cgi extension are executable. Standard Apache installations assume that .cgi extensions are executable files.

  3. Change book_service.cgi's permissions to allow it to be executed. If you are using a Linux or a *NIX-like system, you should execute this command: chmod 755 book_service.cgi. If you are using Windows, the default permission settings usually allow it to be executed, and you don't have to change any permissions.You should be familiar with your system and the HTTP server and set the permissions accordingly. If not, ask your local system administrator; he or she should be able to help you with this.

After the installation is complete, place the client script in any user directory, so that you can execute the client application.

Accessing the Web Service

After our web service is installed and the local web server is running, we're ready to try and access our web service. Remember, the web service client requires a command-line argument that contains an ISBN number. So, we can access the web service by launching the client and passing in a command-line argument:

ch10_soap_client.pl 0201615711 

As expected, we receive the following reply after executing the web service client:

Now searching for a book with ISBN # 0201615711

name: Network Programming with Perl
publisher: Addison Wesley
author: Lincoln D. Stein
pages: 754
price: 39.99 

Note that the web service client can easily be modified to access a different web service. In this example, the location of the web service was hard coded in the client using the following line:


It would be a very simple change to extend our client application to access a different web service, perhaps passed in as a second command-line argument.

As you can see, web services using the SOAP::Lite Perl are a very powerful technology that are easily implemented, and they don't require a lot of extra programming on either the client or server side. SOAP is by far the most popular web service technology in use today.

  • + Share This
  • 🔖 Save To Your Account