Joel Hainley : San Francisco Bay Area Software Consultant

  • rss
  • Home
  • About
  • Reading Lists
    • 2008 Reading List
    • 2007 Reading List
    • 2006 Reading List
    • 2005 Reading List
    • 2004 Reading List
    • 2003 Reading List
    • 2002 Reading List
    • 2001 Reading List
    • 2000 Reading List
    • 1999 Reading List
    • 1998 Reading List
  • Software
    • mcalc - a tournament poker utility
    • Joel’s "Super Fancy"(tm) Maximum Heart Rate [MHR] Worksheet

PHP Soap - A Simple PHP Web Service Example

March 3, 2008

There’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 :

function getRot13($pInput){
$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 :

// turn off the WSDL cache
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 ;-)

Comments
4 Comments »
Categories
php, programming
Tags
php, php soap example, php web services, php5, soap, soap client, soap server, web services
Comments rss Comments rss
Trackback Trackback

Navigation

  • .NET
  • 30-Day-Challenge
  • Amateur Radio
  • bicycle
  • books/reading
  • business
  • c#
  • cackl
  • Exercise
  • flex
  • friends
  • hamtesting.com
  • knuth
  • lectures
  • life
  • observations
  • people watching
  • personal finance
  • philosophy
  • php
  • picprep
  • programming
  • reading
  • santa barbara
  • scheme
  • school
  • seo
  • uISV
  • Uncategorized
  • web services
  • website

Search

Archives

  • December 2008
  • November 2008
  • October 2008
  • September 2008
  • August 2008
  • July 2008
  • May 2008
  • April 2008
  • March 2008
  • February 2008
  • January 2008
  • November 2007
  • October 2007
  • September 2007
  • July 2007
  • June 2007
  • May 2007
  • April 2007
  • March 2007
  • January 2007
  • October 2006
  • September 2006
  • August 2006
  • July 2006
  • June 2006

Site Pages

  • About
  • Reading Lists
    • 1998 Reading List
    • 1999 Reading List
    • 2000 Reading List
    • 2001 Reading List
    • 2002 Reading List
    • 2003 Reading List
    • 2004 Reading List
    • 2005 Reading List
    • 2006 Reading List
    • 2007 Reading List
    • 2008 Reading List
  • Software
    • Joel’s "Super Fancy"(tm) Maximum Heart Rate [MHR] Worksheet
    • mcalc - a tournament poker utility

friends

  • Ashley McNamara Photography
  • Colin McNamara

my websites

  • HamTesting.com
  • Photos
rss Comments rss valid xhtml 1.1 design by jide powered by Wordpress get firefox