PHP Soap - A Simple PHP Web Service Example
March 3, 2008There’s no doubt that today web services are hot, all of the commercial platforms ( J2EE, .Net, etc ) have tools that make the development of web services very easy. There are also a bunch of tools that are out there that help people generate their WSDL’s and can generally ease all of the difficult parts of developing web services. The great thing is that with the release of PHP 5 we now have tools that allow us to quickly develop web services in PHP. The great thing about this is that you can stub out api’s and start to have different solutions speak easily among themselves, share business logic to languages and technologies that are “not native”.
Within this article I’m just going to hit some highlights about creating your own web services using SOAP and PHP. I will also provide a URL to a web service that I have written for people to fiddle around with their first SOAP client. Please note that I’m not providing an exhaustive example of all of the details of wsdl’s/SOAP and the like, this is the SOAP equivalent of a “hello world” program, but with parameter passing included since most “Hello Worlds” don’t have parameters passed into them. We’re going to be building a little service that will support 2 operations, rot13 of a supplied string, and the mirroring(reversing) of a supplied string. Nothing terribly fancy, but certainly more interesting than “hello world” ;-).
You can see the WSDL for this service here . There isn’t much to note with the WSDL it is a simple service and I’m not trying to get too crazy here. So take a look at it and see if it makes sense, if not, try to compare it against the code samples below and see if you can determine how it all relates.
Now that we have the WSDL defined it’s time to build the SOAP server to support this WSDL. Here’s the full listing for the server :
$rot = str_rot13($pInput);return($rot);
}
function getMirror($pInput){
$mirror = strrev($pInput);
return($mirror);
}
// turn off the wsdl cache
ini_set(“soap.wsdl_cache_enabled”, “0″);
$server = new SoapServer(“scramble.wsdl”);
$server->addFunction(“getRot13″);
$server->addFunction(“getMirror”);
$server->handle();
Pretty easy to understand, you’ll notice that we are just defining the functions at the top, then once we have created the soap server using the WSDL we simply associate our php functions with the functions defined in the WSDL by passing the function name to the addFunction message of the newly created SOAP server. Note that we have turned off the caching of the WSDL, this is something you’ll wanna do while you’re developing. When you don’t do this and make changes to your WSDL and they never seem to take, it can get frustrating ;-).
Ok with the server out of the way let’s turn our attention to the client. Here’s the listing for a simple client :
ini_set(“soap.wsdl_cache_enabled”, “0″);
$client = new SoapClient(“http://www.joelhainley.com/examples/soap/scramble.wsdl”);
$origtext = “mississippi”;
print(“The original text : $origtext
“);
$scramble = $client->getRot13($origtext);
print(“The scrambled text : $scramble
“);
$mirror = $client->getMirror($scramble);
print(“The mirrored text : $mirror
“);
I’m sure you could write a fancier example, but it gets the point across. First, You turn off the WSDL caching on your client as well as the server. Then you create a new soap client using the wsdl. Then you just start calling methods that you defined in the server. That’s about all there is to it.
Note, I’m going to keep the Soap Server referenced in this article up for you all to use. Please feel free to use it for testing your clients as you’re getting up and running. Eventually you’ll wanna start using your own servers for more interesting things though ![]()






If you are claiming that .NET and Java have tools
Johnny G | March 20, 2008If you are claiming that .NET and Java have tools that make the development of web services easy, I would argue you are wrong. Using SOAP with .NET and Java is one of the most frustrating things I have ever done. As one uses RPC encoding, another uses a Document format, indeed they are not very compatible. If you then throw in the different versions of AXIS, .NET framework, etc… It’s a jungle out there, a simple google search will validate my claims.
Cheers for the article dude. I was looking for a
John-Boy | August 7, 2008Cheers for the article dude. I was looking for a good, clear intro to PHP and Web Services as I study towards PHP certification and found what I needed here.
Hello, Nice example :-) How can I use arrays at my client-page? Example: Client-page: $myArray
Bryan | August 31, 2008Hello,
Nice example
How can I use arrays at my client-page?
Example:
Client-page:
$myArray = $client->TestArray(5, 7, 5);
print(“The array-text : $myArray);
Server-page:
function TestArray($intVal1, $intVal2, $intVal3)
{
echo ($intVal1 + $intVal2 + $intVal3)
}
Hope I can help
Bryan
Uuups ... forget "Hope I can help"....I mean....."Hope YOU can
Bryan | August 31, 2008Uuups … forget “Hope I can help”….I mean…..”Hope YOU can help”