RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Consuming PHP Web Services with AJAX  : Page 4

Discover how to write PHP web services and consume them through SOAP-AJAX calls.


Testing the JavaScript/AJAX Client

Now for the fun part: testing the JavaScript/AJAX client. Open your browser and browse to the http://localhost/php/webservice/ URL. You should see something similar to Figure 3.

Figure 3. Testing the JavaScript/AJAX Client: Using the client interface, you can enter rectangle width and height values and get the area.

Insert two integers in the two text fields (For example, type 40 for l and 120 for L) and press the Calculate Area button. You should get something like Figure 4.

Figure 4. Rectangle Area: When you enter two integer values and press the Calculate Area button, you'll see the area result appear below the button.

Monitoring the SOAP request-response mechanism

If you want to see the HTML headers and SOAP messages sent behind the scenes, there are dedicated applications available. One simple approach uses "Firebug," a free plug-in for Firefox. For example, Figure 5 shows the HTML request/response headers that were sent and received by the AJAX client to the nusoap_server.php web service:

Figure 5. HTML Headers: Here's a partial view of the headers posted to the web service.

Similarly, you can monitor the complete SOAP request (see Figure 6), and response (see Figure 7)

Figure 6. SOAP Request: Here's the raw SOAP message posted to the web service.
Figure 7. SOAP Response: Here's the raw SOAP response from the web service.
Calling web services from AJAX is an interesting and natural approach, considering that web services typically respond with string messages, numbers, or images in various formats (such as SVG), while developers typically use AJAX in web pages to retrieve and display strings, numbers, and images—exactly the same kind of information. Putting the two together is a good technique that will help you build cool interactive web sites.

Octavia Andreea Anghel is a senior PHP developer currently working as a primary trainer for programming teams that participate at national and international software-development contests. She consults on developing educational projects at a national level. She is a coauthor of the book "XML Technologies—XML in Java" (Albastra, ISBN 978-973-650-210-1), for which she wrote the XML portions. In addition to PHP and XML, she's interested in software architecture, web services, UML, and high-performance unit tests.
Email AuthorEmail Author
Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date