PHP and ASP.NET: Playing Well with Others

No matter how complete your mastery of a given programming language or platform, sometimes you need to use code that doesn't run in your favorite environment. This may be a legacy library, or even code maintained by someone else. Somehow you'll need to call code running in another language, perhaps on a completely different machine. There are many ways to do this, but SOAP and WSDL provide one of the simplest, easiest to implement, and most widely supported mechanisms. In this article, we'll discuss how to use SOAP and WSDL and we'll show some simple examples of how PHP and ASP.NET 2.0 can use these standards to communicate over a network.

To do the examples in this tutorial you'll need the following:

  • Visual Studio 2005
  • PHP 5 (with the standard SOAP libraries installed and enabled)

It is possible that PHP and Visual Studio may be running on different machines for the purposes of the example in this article. If so, these machines need to be able to access each other over the network. The example in this article was created using Visual Studio with code running in the Development Server and PHP 5.0 running under IIS on the same machine.

For this article we'll assume you're familiar with PHP and ASP.NET, or have read "A PHP Developer's Guide to ASP.NET 2.0." The Visual Studio development will be done in C#, but you don't need to understand too much C# to follow along.

SOAP and WSDL

SOAP (Simple Object Access Protocol) is a protocol for sending XML messages over a network. These messages can be used to perform Remote Procedure Calls (RPC). Though HTTP is the most commonly used transport mechanism for SOAP, you can use SMTP, FTP, or other more exotic mechanisms such as Jabber, if you wish. A given SOAP server can support any number of different methods, which can be called by clients. A Web Services Description Language (WSDL) document is a formal description of the properties of a given SOAP server, written in XML. It describes supported methods, data types for arguments, and return values for each method. It is very similar to an interface in C# or a header file in C; it is a contract describing the behavior of a set of methods.

The WSDL file provides both a human-readable (if you can read XML) and machine-readable interface server specification. A SOAP server does not require a WSDL document, but one can be used to simplify access to the methods it describes. Both Visual Studio and the PHP-5-native SOAP objects can automatically generate proxy classes from WSDL documents. One downside of WSDL documents is that loading them over the network can cause delays in method calls. WSDL caching minimizes this problem.

To demonstrate SOAP interoperability, we'll create servers in both PHP and ASP.NET that return random numbers within a specified range. We'll then create clients that call these servers.