Home > php, programming > PHP Soap – A Simple PHP Web Service Example

PHP Soap – A Simple PHP Web Service Example

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 ;-)

  1. Johnny G
    March 20th, 2008 at 06:28 | #1

    If 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.

  2. August 7th, 2008 at 11:46 | #2

    Cheers 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.

  3. Bryan
    August 31st, 2008 at 02:41 | #3

    Hello,

    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

  4. Bryan
    August 31st, 2008 at 02:42 | #4

    Uuups … forget “Hope I can help”….I mean…..”Hope YOU can help”

    :-)

  5. Sirish
    December 29th, 2008 at 03:54 | #5

    nice article dude. Now i got a hint about SOAP. Isn’t it an aid for using a function present in a different web server??

  6. joelhainley
    December 31st, 2008 at 09:07 | #6

    At it’s most basic, yes SOAP is an aid to providing access to functions on different servers. SOAP provides the definitions for how the function and it’s parameters and return values are defined. SOAP also provides for the creation of custom data types for sending data to and from the server, and a few other odds and ends.

  7. mike
    February 3rd, 2009 at 15:45 | #7

    finaly clear, simple and working example. Thanx a lot.
    By the way correct -> characters :)

  8. ict android
    July 27th, 2009 at 14:24 | #8

    thanks! nice buddy!

  1. No trackbacks yet.