Friday, December 24, 2010

Introduction to NuSOAP



NuSOAP is a group of PHP classes that allow developers to create and consume SOAP web services. It does not require any special PHP extensions. The current release version (0.6.7) of NuSOAP at the time this was written (03-November-2004), supports much of the SOAP 1.1 specification. It can generate WSDL 1.1 and also consume it for use in serialization. Both rpc/encoded and document/literal services are supported. However, it must be noted that NuSOAP does not provide coverage of the SOAP 1.1 and WSDL 1.1 that is as complete as some other implementations, such as .NET and Apache Axis.


This document describes how to obtain and install NuSOAP, then provides some samples to illustrate the capabilities of NuSOAP. It is by no means an exhaustive description of NuSOAP, but it is hopefully enough to get any PHP coder started. Programming with NuSOAP follows this up with more complex samples.


Installation

Hello, World

Debugging

Request and Response

Resources


Installation


The NuSOAP project is hosted by SourceForge. You can obtain NuSOAP either as a downloadable release or from the CVS tree of the NuSOAP project on SourceForge. A browser-based CVS interface is provided. As an example, you can obtain version 1.81 of nusoap.php with the following URL: http://cvs.sourceforge.net/viewcvs.py/*checkout*/nusoap/lib/nusoap.php?rev=1.81. You can start working with the version of the code you choose. I have run the samples below with version 1.81.


Once you have downloaded a copy of nusoap.php, you simply need to place it in your code tree so that you can include it from your PHP code. Some users put in it a separate lib directory. For my examples, I placed it in the same directory as the sample code itself.


Return to top.

Hello, World


Showing no imagination whatsoever, I will start with the ubiquitous "Hello, World" example. This will demonstrate the basic coding of NuSOAP clients and servers.


I will start with the server code, since without a server there is no need to have any client. The server exposes a single SOAP method named Hello, which takes a single string parameter for input and returns a string. Hopefully, the comments within the code provide sufficient explanation! For the simplest services, all that will change are the functions being defined and registered.


<?php  


// Pull in the NuSOAP code require_once('nusoap.php');
// Create the server instance $server = new soap_server;
// Register the method to expose $server->register('hello');
// Define the method as a PHP function
function hello($name) {
return 'Hello, ' . $name; }

// Use the request to (try to) invoke the service
$HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA)
? $HTTP_RAW_POST_DATA : '';
$server->service($HTTP_RAW_POST_DATA);

?>

The client for this service is below. There are a few important things to note. First, when the instance of soapclient is created, the parameter specified is the URL to the service. In my case, helloword.php is accessed from http://localhost/phphack. The services you consume will, of course, be located at different URLs. Second, when calling the service, the first parameter is the service name. This must match the case-sensitive name of a method available on the server. In this example, it must match the method registered within helloworld.php. Finally, the second parameter in the call is an array of parameters that will be passed to the SOAP service method. Since the hello method of helloworld.php requires a single parameter, this array has one element.


<?php  

// Pull in the NuSOAP code
require_once('nusoap.php');

// Create the client instance

$client = new soapclient('http://localhost/phphack/helloworld.php');

// Call the SOAP method
$result = $client->call('hello', array('name' => 'Scott'));

// Display the result

print_r($result);

?>

Return to top.

Debugging


As with all programming, there are times when you will have problems you need to debug. NuSOAP provides a couple of facilities to help with that. With SOAP, a very typical step in debugging is to view the request being sent and response being returned. The NuSOAP soapclient class has members named request and response that allow you to view these. For example, here is a modified version of the helloworldclient.php that displays the request and response. In the next section, I will review the structure of the request and response displayed by this client code.


<?php  

// Pull in the NuSOAP code
require_once('nusoap.php');

// Create the client instance
$client = new soapclient('http://localhost/phphack/helloworld.php');

// Call the SOAP method
$result = $client->call('hello', array('name' => 'Scott'));

// Display the result
print_r($result);


// 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>';

?>

NuSOAP also provides a means to view debug information logged throughout its classes. Adding the following to the client code will display this verbose debugging information. Unfortunately, interpretation of the output must be left to the reader.


// Display the debug messages  

echo '<h2>Debug</h2>';
echo '<pre>' . htmlspecialchars($client->debug_str, ENT_QUOTES)
. '</pre>';

The server can provide similar debugging. Interestingly, the debug text is written as an XML comment at the end of the SOAP response, so it can be viewed by displaying the response at the client. The server with debugging enabled looks like this.


<?php  

// Pull in the NuSOAP code
require_once('nusoap.php');

// Enable debugging *before* creating server instance
$debug = 1;

// Create the server instance
$server = new soap_server;

// Register the method to expose
$server->register('hello');

// Define the method as a PHP function

function hello($name) {
return 'Hello, ' . $name; }

// Use the request to (try to) invoke the service

$HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA)
? $HTTP_RAW_POST_DATA : '';
$server->service($HTTP_RAW_POST_DATA);

?>

A third means of debugging is not really debugging at all. It is simply good programming practice. The examples above have not checked for errors occurring during the SOAP call. A more robust client would look like the following.


<?php  
// Pull in the NuSOAP code
require_once('nusoap.php');

// Create the client instance
$client = new soapclient('http://localhost/phphack/helloworld.php');

// Check for an error
$err = $client->getError();
if ($err) {
// Display the error
echo '<p><b>Constructor error: ' . $err . '</b></p>';

// At this point, you know the call that follows will fail }
// Call the SOAP method
$result = $client->call('hello', array('name' => 'Scott'));

// Check for a fault
if ($client->fault) {
echo '<p><b>Fault: ';
print_r($result);
echo '</b></p>';
} else {
// Check for errors
$err = $client->getError();
if ($err) {
// Display the error
echo '<p><b>Error: ' . $err . '</b></p>';
} else {
// Display the result
print_r($result); } }

?>

To test this code, cause an error to happen, such as by changing the name of the method to invoke from 'hello' to 'goodbye'.


Return to top.

Request and Response


I showed above how easy it is to display the SOAP request and response. Here is what the request from the helloworld2client.php looks like.


POST /phphack/helloworld2.php HTTP/1.0  
Host: localhost User-Agent: NuSOAP/0.6.8 (1.81)
Content-Type: text/xml; charset=ISO-8859-1
SOAPAction: "" Content-Length: 538

<?xml version="1.0" encoding="ISO-8859-1"?>
<SOAP-ENV:Envelope
SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:si="http://soapinterop.org/xsd">
<SOAP-ENV:Body>


<ns1:hello xmlns:ns1="http://testuri.org">
<name xsi:type="xsd:string">Scott</name>
</ns1:hello>
</SOAP-ENV:Body> </SOAP-ENV:Envelope>

In the HTTP headers, you will notice that the SOAPAction is an empty string. This is simply the default. Your service methods can be assigned to a SOAPAction, and your client code can specify a SOAPAction as one of the parameters to the call method.


In the XML payload, you see that NuSOAP uses ISO-8859-1 character encoding, also known as Latin-1. To specify a different encoding, you set the soap_defencoding property on the soapclient instance. It is the programmer's responsibility to encode the parameter data correctly using the specified encoding. Fortunately, PHP provides functions for encoding and decoding data using the most common encoding in SOAP, UTF-8.


Another thing to note is that the element specifying the method call, the element named hello, has been placed in the http://tempuri.org namespace. It is better practice, and necessary with many services, to specify the actual namespace in which the method is defined. This will be shown in a future document.


The response from the SOAP service looks like this.


HTTP/1.1 200 OK  Server: Microsoft-IIS/5.0  
Date: Wed, 03 Nov 2004 21:32:34 GMT
X-Powered-By: ASP.NET
X-Powered-By: PHP/4.3.4
Server: NuSOAP Server v0.6.8
X-SOAP-Server: NuSOAP/0.6.8 (1.81)
Content-Type: text/xml;
charset=ISO-8859-1
Content-Length: 556

<?xml version="1.0" encoding="ISO-8859-1"?>
<SOAP-ENV:Envelope
SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:si="http://soapinterop.org/xsd">
<SOAP-ENV:Body>
<ns1:helloResponse xmlns:ns1="http://tempuri.org">
<return xsi:type="xsd:string">Hello, Scott</return>
</helloResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

Return to top.

You can download the source for this example as well.


Return to top.

Monday, December 20, 2010

10 network diagramming tools for every budget

1-CADE (free)



Cade is a tool developed by a company that focuses primarily on CAD. CADE is a 2D vector editor for Windows that can handle the task of diagramming your network with ease. One feature I really like about this tool is the ability to notate IP address, serial number, and manufacturer for each piece of equipment. CADE has all the stencils you need for a complete network diagram. CADE is free to use.

2-Concept Draw Pro



Concept Draw is one of the more powerful network diagramming tools you will find. Concept Draw is actually more than just a network diagramming tool, it's a full-fledged business diagramming tool. Concept Draw Pro is full drag and drop with little to no learning curve. This particular tool has every networking symbol you will need as well as the ability to customize just about everything you add. The cost of Concept Draw Pro is $249.


3-Dia (free)



Dia is an open source diagramming tool that suffers from a very old-school look and feel on its symbols and UI. However, Dia is very simple to use and doesn't distract from the task at hand. Dia works on just about any Linux desktop and is free of charge.



4-Diagram Designer (free)



Diagram Designer is another freeware tool that looks a bit outdated but the ease of use should make it appealing to a wide variety of users. Unlike Dia, Diagram Designer has a much larger selection of symbols and icons. The only thing I didn't much care for with Diagram Designer is that the connector tool is a free-form tool so you actually have to manually draw your connections between machines. Outside of that little drawback, DD is a solid entry.



5-eDraw



Outside of the industry standard, Visio, eDraw Max is one of the finest of the group. Along with a very user-friendly interface and next to no learning curve, eDraw Max has one of the more updated look and feels among all of the entries. Of course eDraw Max is also a full-blown diagramming tool that can cover all of your business diagramming needs. The cost of eDraw Max is $99.95 for one license and, as you increase numbers, the individual license cost goes down.



6-GoVisual Diagram Editor (free)



Some pieces of software are simply not made equal. GoVisual Diagram Editor is one such tool that shows how a very complicated tool can produce results that are less than pleasing. Although you can really take some time with GoVisual and make the results work for you, you will be missing features (such as network device object icons) that make the network diagram far easier to read. But if you're looking for a free tool that can be used for just about any type of diagramming, GoVisual might be the tool for you. Cost? Free.



7-LanFlow



LanFlow is one of the better tools I have used for this task. It has a great user interface, plenty of network objects to play with, the ability to create LAN, telecom, Internet, and computer diagrams with little effort. LanFlow even comes with two different network diagram templates (3D and b&w). When you open up a new network diagram template you can simply start dragging around objects to fit your network, add new objects (or remove objects), group (or ungroup) objects, and much more. The cost of LanFlow for a single user license, is $89.00, making this one of the best of the low-cost alternatives to Visio.



8-NetProbe




Although you can use NetProbe as a network diagramming tool, it's primary purpose is a real-time network device monitoring. But what makes NetProbe a possible contender for network diagramming is its ability to allow you to add, as you need them, devices that are on (or will be on) your network. But, you don't have to do all of that manual labor as NetProbe has a built-in tool that will automatically scan your network and lay out all of the devices for you. The Standard version is free but is limited to eight hosts. The Pro version of NetProbe (allowing for up to 20 hosts) will set you back only $40.00 and the Enterprise version (allowing for 400 hosts) runs $295.



9-Network Notepad (free)




Network Notepad is exactly what it sounds like it would be -- a notepad for network diagrams. But don't let the term "notepad" fool you. Network Notepad is full-fledged diagramming tool that even includes interactive functionality (such as telnet, browse, ping, and more). Network Notepad has an easy-to-use drag-and-drop interface and also has an autodiscover feature for Cisco devices. This tool is free of charge.



10-Microsoft Visio




Visio is, of course, the de facto standard for diagramming in the Windows world. Not only can you create beautiful network diagrams, you can create them easily and share data-linked diagrams with others via a Web browser. Visio offers plenty of templates to add on such as data center templates, office consolidation, fault tree, help desk, HVAC, network rack equipment, network planning for enterprise/data center/home office, and much more. Visio is the king of the hill as far as network diagramming, and with the crown comes the price tag of $249.99 for the Standard version, $559.99 for the Professional version, and $999.99 for the Premium 2010 edition. For more information on what each version offers, visit the Visio product purchase page.