Browse DevX
Sign up for e-mail newsletters from DevX


Consuming PHP Web Services with AJAX  : Page 2

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




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Verifying Web Services

One nice feature of NuSOAP is that it lets you verify that a web service exists and ensure that it exposes the methods you need—without having to develop a client. For example, accessing the URL http://localhost/php/webservice/nusoap_server.php link reveals the web service WSDL for the web service you just created (note that you didn't have to write or generate a WSDL—but the WSDL is there, nonetheless), and the service methods. Next to each method name you can see a list containing the information you need to write a proper web service client. Figure 1 shows a screen capture of the WSDL and methods for the nusoap_server.php sample web service.

Figure 1. Verifying Web Services: Accessing the web service URL reveals the WSDL file and the service methods.

Writing a Web Service Client

With the server completed and verified, you can write a NuSOAP web service client. This is a process even simpler than creating a server, as you will see in this section. The steps are:

  1. Start by creating a client instance by instantiating the soapclientNusoap class. Typically, you will use the class constructor that takes the WSDL URL and a Boolean value indicating that you want to use a WSDL file (NuSOAP also lets you create web services without WSDLs, but that aspect is not discussed here).
  2. // Create the client instance $client = new soapclientNusoap( 'http://localhost/php/webservice/nusoap_server.php?wsdl', true);

  3. It's best practice to check for errors at this point, for example:
  4. // Check for an error $err = $client->getError(); if ($err) { // Display the error echo '<h2>Constructor error</h2><pre>' . $err . '</pre>'; // At this point, you know any calls to // this web service's methods will fail }

  5. Finally, you can call the web service methods using the NuSOAP call method. The following examples call the TriangleArea and RectangleArea methods (notice that they also check for potential errors).

  6. // Call the TriangleArea SOAP method $result = $client->call('TriangleArea', array('b' => 10, 'h' => 15)); // Check for a fault if ($client->fault) { echo '<h2>Fault</h2><pre>'; print_r($result); echo '</pre>'; } else { // Check for errors $err = $client->getError(); if ($err) { // Display the error echo '<h2>Error</h2><pre>' . $err . '</pre>'; } else { // Display the result echo '<h2>Result</h2><pre>'; print_r($result); echo '</pre>'; } } // Call the RectangleArea SOAP method $result = $client->call('RectangleArea', array('L' => 40, 'l' => 20)); // Check for a fault if ($client->fault) { echo '<h2>Fault</h2><pre>'; print_r($result); echo '</pre>'; } else { // Check for errors $err = $client->getError(); if ($err) { // Display the error echo '<h2>Error</h2><pre>' . $err . '</pre>'; } else { // Display the result echo '<h2>Result</h2><pre>'; print_r($result); echo '</pre>'; } }

    Figure 2. Web Service Client: Browsing to the nusoap_client.php client page shows the results of the TriangleArea and RectangleArea calls made from that page as well as the request and response details.
  7. Optionally, you can display the requests/responses like this:

// Display the request and response echo '<h2>Request</h2>'; echo '<pre>' . htmlspecialchars($client->request, ENT_QUOTES) . '</pre>'; echo '<h2>Response</h2>'; echo '<pre>' . htmlspecialchars($client->response, ENT_QUOTES) . '</pre>';

When you put the four steps described above together to create a web service client, the results will look like Listing 2 (save the code in Listing 2 to the webservice folder):

Now, you can test your first NuSOAP web service by opening the http://localhost/php/webservice/nusoap_client.php URL in a browser. The output should look like Figure 2.

At this point, you've built a web service, exposed its methods, and consumed it on the server using PHP code. The next section discusses how to consume a web service from a JavaScript AJAX client.

Thanks for your registration, follow us on our social networks to keep up-to-date